• I’m attempting to automatically assign terms from a parent post to automatically generated child pages. I am able to detect all children for the given post and can set a term however when attempting to collect the post terms of the parent post, wordpress returns an empty array. All post terms are set in the parent post on initial set up but are not passed into the variable i’ve created.

    see below for sample function added to my functions.php.

    <?php  
    add_action('save_post', 'assign_parent_terms',10);
    
    function assign_parent_terms($post_id){
        $currentpost = get_post( $post_id );;
    
        if(isset($post) && $post->post_type != 'campaign'){
            return $post_id;}
    
        // get all assigned terms
    
        // check nonces and capabilities here.
    
        // use a ternary statement to make sure the input is actually set
        // remember that <code>save_post</code> fires on every post save (any post type).
        // Returns Array of Term ID's for "my_taxonomy".
    
        $term_list = wp_get_object_terms( $currentpost->ID, 'brand');
    
        //$term_list = wp_get_post_terms( $post->ID, 'brand');
        $getchilargs = array(
            'post_parent' => $post->ID,
            'post_type'   => 'campaign', 
            'numberposts' => -1,
            'post_status' => 'any' 
        );
        $children = get_children( $getchilargs );
    
        if(!empty($children)){
            foreach ($children as $child){
                $childID = $child->ID;
                //$childparent = $child->parent;
                //$term_list = $post->post_name;
                wp_set_object_terms($childID, $term_list, 'brand');
            }
    
        }
    
    }
    ?>
    • This topic was modified 4 years, 7 months ago by bcworkz. Reason: code fixed
Viewing 4 replies - 1 through 4 (of 4 total)
  • There seems to be some code missing.
    At the top, you have
    if(isset($post) && $post->post_type != ‘campaign’){
    but there is no variable called $post. It is referenced later also.
    Also, this function is hooked to save_post but it doesn’t check if it’s AUTOSAVE or draft. And when does this hook actually run? Is it during the save or after? Just wondering for the get_post() to work.

    Thread Starter jet89

    (@jet89)

    So the current workflow is as follows. I have one function that will create child posts using wp_insert_post and the ‘save_post’ hook when saved prioritized before the included function. After that, I call this function to assign the terms to those child pages.

    I updated the code to accommodate the the variable issue but am still getting the same results.

    It looks like I am able to get all the children pages and apply the term list however the term list variable returns no values.

    revised code below:

    <?php
    add_action(‘save_post’, ‘assign_parent_terms’,10);

    function assign_parent_terms($post_id){
    global $currentpost;
    $currentpost = get_post( $post_id );

    if(isset($currentpost) && $currentpost->post_type != ‘campaign’){
    return $post_id;}

    // CREATE POST CONDITION
    if ( !wp_is_post_revision( $post_id )
    && ‘campaign’ == get_post_type( $post_id )
    && ‘auto-draft’ != get_post_status( $post_id ) ) {
    $currentpost = get_post( $post_id );
    // get all assigned terms

    $term_list = wp_get_object_terms( $currentpost->ID, ‘brand’);

    //$term_list = wp_get_post_terms( $post->ID, ‘brand’);
    $getchilargs = array(
    ‘post_parent’ => $currentpost->ID,
    ‘post_type’ => ‘campaign’,
    ‘numberposts’ => -1,
    ‘post_status’ => ‘any’
    );
    $children = get_children( $getchilargs );

    if(!empty($children)){
    foreach ($children as $child){
    $childID = $child->ID;
    //$childparent = $child->parent;
    //$term_list = $post->post_name;
    wp_set_object_terms($childID, $term_list, ‘brand’);
    }
    }

    }
    }
    ?>

    • This reply was modified 4 years, 7 months ago by jet89.
    • This reply was modified 4 years, 7 months ago by jet89.
    Moderator bcworkz

    (@bcworkz)

    The “save_post” action also passes the post object and a boolean for whether the post is being updated or not. There’s no need to get_post(). In fact when you try to do so you run into a race condition because the DB has not yet had time to update the DB before your code tries to get the post from the DB. Collect all the passed data with something like:

    add_action('save_post', 'assign_parent_terms', 10, 3 );
    
    function assign_parent_terms( $post_id, $post, $update ){

    When you post code in these forums, please demarcate with backticks or use the code button. Otherwise the forum’s parser corrupts your code. I fixed your OP code for you.

    • This reply was modified 4 years, 7 months ago by bcworkz. Reason: typo
    Thread Starter jet89

    (@jet89)

    thanks for the notes, i implemented this as well as using wp_get_post_terms instead and the results are as desired!

    Thank you for all the help everyone!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Detect Terms of Current Post on Save’ is closed to new replies.