admin管理员组文章数量:1410731
I am using Yoast SEO Premium to handle the selection of Primary Categories. I am currently working with a Listing (listing) custom post type with a hierarchal taxonomy of Listing Category (listing_cat). I need the topmost parent to be auto-selected as the Primary Category on Publish or Edit.
The code I have tried is below. It auto-selects the parent item as a category when its child category is selected, but I need to take it a step further. I feel I am close, but missing something.
/* Auto Select Listing Parent Category on Child Category Select */
add_action('save_post', 'am_assign_listing_parent_terms', 10, 2);
function am_assign_listing_parent_terms($post_id, $post){
if($post->post_type != 'listing')
return $post_id;
// get all assigned terms
$terms = wp_get_post_terms($post_id, 'listing_cat' );
foreach($terms as $term){
while($term->parent != 0 && !has_term( $term->parent, 'listing_cat', $post )){
// move upward until we get to 0 level terms
wp_set_post_terms($post_id, array($term->parent), 'listing_cat', true);
$term = get_term($term->parent, 'listing_cat');
// Update Yoast SEO Post Meta with Primary Category
update_post_meta($post_id,'_yoast_wpseo_primary_listing_cat',$term);
}
}
}
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 have referenced this article: Set Primary category using the Yoast SEO plugin - but I need the exact opposite of what it does.
Any help is appreciated.
本文标签: phpAutoSelect Parent Category as Primary
版权声明:本文标题:php - Auto-Select Parent Category as Primary 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745036998a2638848.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论