Skip Navigation

[Resolved] Function to assign content template from API import

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to programmatically assign a Content Template to imported posts.

Solution: Content Template assignment is defined by a hidden custom field "_views_template" that holds the ID of the Content Template to apply to a given post. You can use the save_post hook to apply this value automatically whenever a post is saved:

function save_book_meta( $post_id, $post, $update ) {
 
    $post_type = get_post_type($post_id);
 
    // If this isn't a 'book' post, don't update it.
    if ( "book" != $post_type ) return;
 
    // - Update the post's metadata.
    update_post_meta( $post_id, '_views_template', 12345 );
}
add_action( 'save_post', 'save_book_meta', 10, 3 );

Relevant Documentation:
https://codex.wordpress.org/Plugin_API/Action_Reference/save_post

This support ticket is created 6 years, 5 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 3 replies, has 2 voices.

Last updated by Christian Cox 6 years, 5 months ago.

Assisted by: Christian Cox.

Author
Posts
#916260

Hi there,

I have an external XML feed setup which creates posts on my website. However, each time the import runs, the updated posts lose there association with the Single Post Content Template I've setup - it just goes back to "None".

Is there a way to force this. I.e. is there a hidden field / variable that I can set in the Post Fields Group? Or a PHP function which looks at when these specific Posts are first created and assigns it then?

Thanks in advance

Paul

#916321

Yes, there is a hidden postmeta field associated with each post that determines which Content Template is applied to the post. It is _views_template. If you import posts and you want to assign a specific Content Template to those posts, you can set the postmeta value for the hidden _views_template field. If you update posts and delete or modify this template postmeta value, the original Content Template will no longer be assigned to the post.

#916554
Screenshot(47).png

PLease see attachment....

So is it as simple as applying the name of the CT to the hidden field?

Or would I need a function to run on Post Save - if so is there an example in the Support Library of this?

Many thanks

Paul

#917112

The value should be the Content Template ID, not the slug. Here's a sample that saves the template ID for a "book" custom post:

function save_book_meta( $post_id, $post, $update ) {

    $post_type = get_post_type($post_id);

    // If this isn't a 'book' post, don't update it.
    if ( "book" != $post_type ) return;

    // - Update the post's metadata.
    update_post_meta( $post_id, '_views_template', 12345 );
}
add_action( 'save_post', 'save_book_meta', 10, 3 );