Created
February 7, 2019 16:20
-
-
Save michelmany/67cbb33fa168647d5dda68554539e14e to your computer and use it in GitHub Desktop.
WordPress - Set make primary using YOAST SEO. (Set default term for the post)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// **** Set default terms to all Case Studies Custom Posts | |
function set_default_custom_post_terms( $custom_post, $taxonomy, $first_term_slug, $second_term_slug, $primary_term ) { | |
$all_posts_ids = get_posts(array( | |
'fields' => 'ids', | |
'posts_per_page' => -1, | |
'post_type' => $custom_post | |
)); | |
$first_term_id = get_term_by( 'slug', $first_term_slug, $taxonomy )->term_id; | |
$second_term_id = get_term_by( 'slug', $second_term_slug, $taxonomy )->term_id; | |
$primary_term_id = get_term_by( 'slug', $primary_term, $taxonomy )->term_id; | |
function wpseoPrimaryTerm($tax, $postID, $term){ | |
if ( class_exists('WPSEO_Primary_Term') ) { | |
// Set primary term. | |
$primaryTermObject = new WPSEO_Primary_Term($tax, $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'; | |
} | |
} | |
foreach ($all_posts_ids as $key => $term_id) { | |
wp_set_post_terms( $term_id, [$first_term_id, $second_term_id], $taxonomy ); | |
wpseoPrimaryTerm( $taxonomy, $term_id, $primary_term_id ); | |
} | |
} | |
set_default_custom_post_terms('case-study', 'catinsight', 'resources', 'case-study', 'case-study'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment