Contact Form 7 and Custom post type

MattM picture MattM · Jan 9, 2015 · Viewed 9.1k times · Source

I want to use contact form 7 in Wordpress to build a order Form. I want the content of the order Form to be populated with content from a custom post type "trade Show Material" - The post type contains the fields "name" "number" "description" "photo" . The idea will be that each piece can be selected from the form . Can anyone offer the general direction for this? Should I perhaps be using another plugin entirely?

Answer

vicente picture vicente · Jan 13, 2015

Maybe you can use the wpcf7_form_tag filter hook for this.

If you want to use a custom post type as the options of a dropdown (select) you can add something like the example below in your functions.php:

function dynamic_field_values ( $tag, $unused ) {

    if ( $tag['name'] != 'your-field-name' )
        return $tag;

    $args = array (
        'numberposts'   => -1,
        'post_type'     => 'your-custom-post-type',
        'orderby'       => 'title',
        'order'         => 'ASC',
    );

    $custom_posts = get_posts($args);

    if ( ! $custom_posts )
        return $tag;

    foreach ( $custom_posts as $custom_post ) {

        $tag['raw_values'][] = $custom_post->post_title;
        $tag['values'][] = $custom_post->post_title;
        $tag['labels'][] = $custom_post->post_title;

    }

    return $tag;

}

add_filter( 'wpcf7_form_tag', 'dynamic_field_values', 10, 2);

In your form you can add the field:

[select* your-field-name include_blank]

In the example above the post_title is used in the options of the dropdown. You can add your own fields here (name, number, description, photo).