0
function set_primary_on_publish ($post_id) {
    global $post; 

    $categories = get_the_terms( $post->ID, 'category' ); 
    // wrapper to hide any errors from top level categories or products without category
    if ( $categories && ! is_wp_error( $category ) ) : 

        // loop through each cat
        foreach($categories as $category) :
          // get the children (if any) of the current cat
          $children = get_categories( array ('taxonomy' => 'category', 'parent' => $category->term_id ));

          if ( count($children) == 0 ) {

                $childid = $category->term_id;
                update_post_meta($post->ID,'_yoast_wpseo_primary_category',$childid);

          }
        endforeach;

    endif;

    }
add_action( 'save_post', 'set_primary_on_publish', 10, 2 );
add_action( 'edit', 'set_primary_on_publish', 10, 2 );
add_action( 'wp_insert_post', 'set_primary_on_publish', 10, 2 );

I'm trying to save the deepest child category as the primary category in the Yoast SEO plugin however the above doesn't seem to do the job.

Anyone who can help me out on this?

Best regards, Dylan Smit

1
  • If it doesn't do what you want, what's the erroneous/actual behaviour of this code?
    – Tom J Nowell
    Commented Sep 19, 2018 at 16:09

2 Answers 2

1

Try instead

update_post_meta($post->ID,'_yoast_wpseo_primary_category',$childid); 

Use below function:

function wpseoPrimaryTerm($taxonomy, $postID, $term){

  if ( class_exists('WPSEO_Primary_Term') ) {
  // Set primary term.
  $primaryTermObject = new WPSEO_Primary_Term($taxonomy, $postID);
  $primaryTermObject->set_primary_term($term);

  // Save primary term.
  $primaryTermObjectAdmin = new WPSEO_Primary_Term_Admin();
  $primaryTermObjectAdmin->save_primary_terms($postID);
  }else{
    echo 'Class WPSEO does not exit';
  }
}

Where $taxonomy - taxonomy name, $PostID - $post->ID, $term - $childid

0

You need to change product to product_cat and _yoast_wpseo_primary_category to _yoast_wpseo_primary_product_cat for it to work. When saving or updating a product it will grab the deepest product_id and set it as the product_id in the wp_postmeta table.

function set_primary_on_publish ($post_ID) {
    global $post;

    $categories = get_the_terms( $post->ID, 'product_cat' ); 

    // wrapper to hide any errors from top level categories or products without category
    if ( $categories && ! is_wp_error( $category ) ) :

        // loop through each cat
        foreach($categories as $category) :

          // get the children (if any) of the current cat
          $children = get_categories( array ('taxonomy' => 'product_cat', 'parent' => $category->term_id ));

          if ( count($children) == 0 ) {
                $childid = $category->term_id;
                update_post_meta($post->ID,'_yoast_wpseo_primary_product_cat',$childid);
          }
        endforeach;
    endif;
}
1
  • Did not you mean "to change 'category' into 'product_cat' and ..."?
    – nmr
    Commented Jul 24, 2019 at 16:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.