最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Set Primary category using the Yoast SEO plugin

programmeradmin3浏览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

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

Share Improve this question edited Sep 19, 2018 at 15:28 Pat J 12.4k2 gold badges28 silver badges36 bronze badges asked Sep 19, 2018 at 15:26 Dylan SmitDylan Smit 133 silver badges7 bronze badges 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
Add a comment  | 

2 Answers 2

Reset to default 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

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;
}
发布评论

评论列表(0)

  1. 暂无评论