PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Adding a Views and Types template when using Gravity Forms to submit a post

  1. Lee Branch
    Member

    Thanks for such a great plugin, love it!

    I also love Views and Types (http://wp-types.com/)

    I am using Gravity Forms to let advertisers create a custom post type (advertisement) on my site. I have set up a view template with views and types to control how these individual posts are displayed.

    Can anyone suggest a way of setting the custom post type submitted through a gravity form to automatically use this template created with views and types?

    My current thinking is that this isn't possible, so I'm planning to set all submitted posts to pending, then manually assigning the template myself via the admin dashboard before publishing. Obviously this is not ideal as it could be tens or hundreds of posts a day.

    Thanks for your help,
    Lee

    Posted 8 years ago on Sunday May 20, 2012 | Permalink
  2. ckrauskopf
    Member

    I am using types and views for a couple of sites too, and I'm just learning myself, but it seems like your use case should be covered by the feature where you can assign a view template to the single version of a custom post type. It's under Views --> Settings - detailed documentation here:
    http://wp-types.com/documentation/user-guides/setting-view-templates-for-single-pages/.

    If that doesn't work for some reason, you could back your way into it. Wordpress will look for an archive template named "single-{posttype}.php". In the Views plugin UI when you create a view-template you can toggle a button to see all the html being output by the plugin, so you could just use that html from the view-template you created to form the basis of your template in single-advertisement.php and Wordpress should automatically apply it to any new advertisements.

    Posted 7 years ago on Thursday June 28, 2012 | Permalink
  3. ckrauskopf
    Member

    I'll just note that I am attempting a rather complicated site where Gravity Forms is used to populated dozens of custom fields and create several different custom post types on form submission. All of these Post types and fields will be controlled by the Types and Views plugins. So if I learn any tricks about integrating the 2 with GForms I'll try to post later. It seems as though Types and Views are also very good plugins (as of course GForms is too).

    Posted 7 years ago on Thursday June 28, 2012 | Permalink
  4. theslink2000
    Member

    Not sure if you're still working at this or not but I've been working on a large project with these three plugins as well and hit the View Template issue this morning but I've solved it.

    Basically even if the settings are set to correctly create the new post type with the template it wasn't working for me when the post was created by GForms. So I got to this code

    // Manually set the View Template for the Surgeries Post Type
    function set_view_template($entry) {
    	update_post_meta($entry['post_id'], "_views_template", 257);  //Update post meta, current post, _views_template is the database field and 257 is the required View Template
    }

    All you need to do is change 257 to the View Template id (visible in the url when you edit it). I also used a similar trick to handle post relationships.

    Hope it helps!

    Posted 7 years ago on Friday August 10, 2012 | Permalink
  5. ckrauskopf
    Member

    I forgot to post here after encountering this myself. As others have found, setting the view template in the normal way apparently doesn't work, but my solution was a bit different.

    Rather than setting the _views_template when creating or updating the post, I'm setting it with a filter when displaying a particular post type. So my code doesn't actually affect Gravity Forms at all. It seems like this has a few advantages for my particular situation. It doesn't matter which form created the post (in case you want to have totally different forms that create the same post type, this code will apply to all of them), and you can create a form that creates posts of multiple different post types on one form submission ( in that case you just need to have one instance of this filter for each post type).

    Disclaimer: I'm not a php expert, so maybe I don't grasp all the nuances and implications of the different approaches. There may be advantages to the other approach or disadvantages to mine that I haven't noticed. I arrived at my approach by working through the Types and Views forums and my excerpt works like a charm on my site.

    Thanks for posting theslink2000 - it's nice to see another approach.

    Here's my code:

    //Set Views Template Custom Post Type Created by GF on the Front End
    add_filter('get_post_metadata', 'meter_template_filter', 1, 4);
    function meter_template_filter($dummy, $post_id, $meta_key, $single) {
    if ($meta_key == '_views_template') {
    if (is_singular('your-custom-post-type-slug')) {
    return 267; //replace 267 with the id of your views template
    }
    }
    return null;
    }
    Posted 7 years ago on Friday August 10, 2012 | Permalink
  6. theslink2000
    Member

    Hi ckrauskopf,

    I never thought about doing it that way, it's really interesting and as you said it gives you a degree of flexibility my solution doesn't. I think it's fair to say your solution is great if you need that extra level of control, such as you suggest, submitting multiple custom post types, and mine is good for just a simple form.

    Oh I forgot to post to add action for my solution.

    add_action("gform_after_submission_7", "set_view_template", 10, 2); // Set's the View Template

    On an unrelated note, you've just given me an idea of how to fix my next issue, using GForms as an edit function!

    Posted 7 years ago on Saturday August 11, 2012 | Permalink

This topic has been resolved and has been closed to new replies.