I have a requirement in WordPress, where , if the taxonomy term string is equal to post title, the update the slug (since the permalink rules fail in this case).
My code is :
add_action('save_post', 'change_default_slug', 10,2);
function change_default_slug($post_id, $post) {
error_log($post_id);
error_log($post->post_title);
error_log($post->post_name);
$taxonomies=get_taxonomies('','names');
$terms = wp_get_post_terms($post->ID, $taxonomies, array("fields" => "names"));
$terms = array_map('strtolower', $terms);
error_log('terms :' . json_encode($terms));
$title = strtolower($post->post_title);
error_log('title : ' . $title);
if(in_array($title, $terms)) {
error_log('yessss');
$args = array (
'ID' => $post->ID,
'post_name' => $post->post_name . "-post"
);
$result = wp_update_post($update_args);
error_log('result');
error_log(json_encode($result));
} else {
error_log('noooooooo');
}
}
On required post I am getting logs : Yesss result 0.
The slug is not updating.
Please help on the same. I have tried literally all solutions available for this issue. It has to be done via functions.php
I was finally able to solve it using : wp_insert_post()
$taxonomies=get_taxonomies('','names');
$terms = wp_get_post_terms($post->ID, $taxonomies, array("fields" => "all"));
foreach($terms as $term) {
if($term->taxonomy == 'category'){
$categories[] = $term->term_id;
} else if($term->taxonomy == 'post_tag'){
$tags[] = $term->term_id;
}
}
.
.
.
//detach the hook to avoid infinite looping of the hook on post insert
remove_action('save_post', 'change_default_slug', 10,2);
//insert post
$result = wp_insert_post($post, true);
//attach post tags to the current post (since not by default attached)
wp_set_post_terms($post_id,$tags,'post_tag');
//attach post categories to the current post (since not by default attached)
wp_set_post_terms($post_id,$categories,'category');
//re-activate the hook
add_action('save_post', 'change_default_slug', 10,2);
Related
In Wordpress, how can I revert to the primary category?
I'm using the following loop, if all three are checked then it just reverts to the last term. I want to make sure it's the primary category.
<?php $term_list = wp_get_post_terms($post->ID, 'category', array("fields" => "names"));
foreach ($term_list as $term) {
$name = $term;
} ?>
This is not a native wordpress feature, but a feature of Yoast SEO (see here).
You can check for primary status the following way:
<?php
$term_list = wp_get_post_terms($post->ID, 'category', ['fields' => 'all']);
foreach($term_list as $term) {
if( get_post_meta($post->ID, '_yoast_wpseo_primary_category',true) == $term->term_id ) {
// this is a primary category
}
}
?>
If you are using custom taxonomies, use the meta_key
_yoast_wpseo_primary_CUSTOM_TAXONOMY
instead.
If you are using "The SEO Framework" plugin instead of "Yoast SEO":
$taxonomy = 'category';
$post_id = get_the_ID();
$terms = wp_get_post_terms($post_id, $taxonomy, ['fields' => 'all']);
$primary_term = intval(get_post_meta( $post_id, '_primary_term_' . $taxonomy, true ));
foreach($terms as $term) {
if( $primary_term == $term->term_id ) {
// this is a primary category
}
}
References:
https://github.com/sybrew/the-seo-framework/blob/4262ea703eaaa50813d8cd4ac13f4537b5c6a4cc/inc/classes/post-data.class.php#L633
Accepted answer won't work if you used Yoast plugin indexing and optimization.
$wpseo_primary_term = new WPSEO_Primary_Term( 'category', get_the_id() );
$wpseo_primary_term = $wpseo_primary_term->get_primary_term();
$term = get_term( $wpseo_primary_term );
This would be correct way to do it.
To make sure site doesn't break if Yoast gets disabled you should wrap the code in
if ( class_exists('WPSEO_Primary_Term') ) { }
If you are using the plugin "Yoast SEO":
yoast_get_primary_term_id( 'product_cat', $post->id );
For anyone using Rank Math SEO plugin, you can use:
get_post_meta( $post_id, 'rank_math_primary_category', true );
Related post: https://wordpress.org/support/topic/get-primary-category-2/
You can use this function to get primary category
<?php
function prefix_get_primary_category($post_id) {
if(class_exists('WPSEO_Primary_Term')) {
$wpseo_primary_term = new WPSEO_Primary_Term( 'category', $post_id );
$wpseo_primary_term = $wpseo_primary_term->get_primary_term();
$the_primary_term = get_term( $wpseo_primary_term );
if(!empty($the_primary_term) && !is_wp_error($the_primary_term)) {
return $the_primary_term;
}
}
return false;
}
I am trying to figure out how to get taxonomy(checkboxes) label from a custom post type that is checked/selected to show on the single custom post. The code below is outputting all the taxonomies not just the checked ones.
function get_terms_chekboxes($taxonomies, $args) {
$terms = get_terms($taxonomies, $args);
foreach($terms as $term){
$output .= $term->name ;
}
return $output;
}
echo get_terms_chekboxes('genre', $args = array('post_type' => 'movie','hide_empty'=>false));
How to get checked taxonomy labels.
Thanks.
Reference
Please try this function for retrieving the term_names of single custom posts assigned to it.
$term_array = array();
$term_list = wp_get_post_terms($post->ID, 'genre', array("fields" => "all"));
foreach($term_list as $term_single) {
$term_array[] = $term_single->name ; //do something here
}
echo implode(", ",$term_array);
where $post->ID is the ID of the single custom post & 'genre' is your taxonomy slug.
I hope, this may be helpful to you.
I am trying to show a list of the taxonomies a custom post type is in, excluding those which aren't hierarchical.
The code below currently works but shows all taxonomies, however I can't get it to check if the taxonomy is hierarchical before running it through the loop.
I know there is a WordPress function that runs this check, but as I usually work front-end, I can't seem figure out where to put it in order for it to take effect:
is_taxonomy_hierarchical( $taxonomy )
The following is the function I am using to output a list of the taxonomies:
// get taxonomies terms links
function custom_taxonomies_terms_links(){
// get post by post id
$post = get_post( $post->ID );
// get post type by post
$post_type = $post->post_type;
// get post type taxonomies
$taxonomies = get_object_taxonomies( $post_type, 'objects' );
$out = array();
echo '<a href="';
echo '/';
echo '">';
echo 'Home';
echo "</a> / ";
foreach ( $taxonomies as $taxonomy_slug => $taxonomy ){
// get the terms related to post
$terms = get_the_terms( $post->ID, $taxonomy_slug );
if (!empty( $terms )) {
foreach ( $terms as $term ) {
$out[] =
'<a href="'
. get_term_link( $term->slug, $taxonomy_slug ) .'">'
. $term->name
. "</a> / ";
}
$out[] = " ";
}
}
return implode('', $out );
}
If I understand your correctly, couldn't you just test the taxonomy like the following:
foreach ( $taxonomies as $taxonomy_slug => $taxonomy ){
if ($taxonomy->hierarchical) {
// get the terms related to post
$terms = get_the_terms( $post->ID, $taxonomy_slug );
if (!empty( $terms )) {
foreach ( $terms as $term ) {
$out[] =
'<a href="'
. get_term_link( $term->slug, $taxonomy_slug ) .'">'
. $term->name
. "</a> / ";
}
$out[] = " ";
}
}
}
When you use 'objects' as the second parameter in:
get_object_taxonomies( $post_type, 'objects' );
what you get back is an array of taxonomy objects, as opposed to just taxonomy names (the other option). The taxonomy object has a property "hierarchical" that indicates whether that taxonomy is hierarchical or not. You can test this to choose the types of taxonomies (hierarchical or not) that you want.
The problem is that I cannot display the category of a post:
http://screencast.com/t/hdQjpSV0Q
I have the following code in a function.php file:
// GET FEATURED IMAGE
function ST4_get_featured_image($post_ID) {
$custom_meta = get_post_custom(get_the_ID());
return $custom_meta["lumen_portfolio_preview_image_image"][0];
}
add_filter('manage_posts_columns', 'ST4_columns_head');
add_action('manage_posts_custom_column', ('ST4_columns_content'), 10, 2);
// ADD NEW COLUMN
function ST4_columns_head($defaults) {
$defaults['featured_image'] = 'Featured Image';
$defaults['categories_portfolio'] = 'Category';
return $defaults;
}
// SHOW THE FEATURED IMAGE
function ST4_columns_content($column_name, $post_ID) {
if ($column_name == 'featured_image') {
$post_featured_image = ST4_get_featured_image($post_ID);
if ($post_featured_image) {
echo '<img style="width:300px;height:200px;" src="' . $post_featured_image . '" />';
}
}
elseif ($column_name == 'categories_portfolio') {
$terms = get_the_terms( $post->ID , 'category' );
foreach ( $terms as $term ) {
$term_link = get_term_link( $term, 'category' );
if( is_wp_error( $term_link ) )
continue;
echo '' . $term->name . '';
}
}
}
I have empty results, NULL, but I want to have the post category field. I used many wordpress functions, but no success.
I've noticed that your using the $post->ID as your parameter in your get_the_terms function, anyways I assume that the problem is you can't retrieve the category of the post, I would recommend using the get_the_category function in place of the get_the_terms in your code:
// SHOW THE FEATURED IMAGE
function ST4_columns_content($column_name, $post_ID) {
if ($column_name == 'featured_image') {
$post_featured_image = ST4_get_featured_image($post_ID);
if ($post_featured_image) {
echo '<img style="width:300px;height:200px;" src="' . $post_featured_image . '" />';
}
}
elseif ($column_name == 'categories_portfolio') {
//replace this $terms = get_the_terms( $post->ID , 'category' );
$terms = get_the_category($post_ID); //should you be using this instead of $post->ID?
foreach ( $terms as $term ) {
//replace this $term_link = get_term_link( $term, 'category' );
$term_link = get_category_link($term->term_id);
if( is_wp_error( $term_link ) )
continue;
echo '' . $term->name . '';
}
}
}
You can do a var_dump($terms) to further see the values being returned by this function, hope it helps, cheers!
I think you are using custom post type "Portfolio" as shown in attached screenshot.
Use wp_get_post_terms rather than get_the_terms
Update following code:
$terms = get_the_terms( $post->ID , 'category' );
to
$terms = wp_get_post_terms( $post->ID , 'category', array("fields" => "all") );
Note that 2nd parameter is the taxonomy name you are using while registering taxonomy.
See an example below where 'testimonials-cat' is used to register taxonomy.
register_taxonomy('testimonials-cat', 'testimonials', $args);
Your code for above taxonomy will be:
$terms = wp_get_post_terms($post->id, 'testimonials-cat', array("fields" => "all"));
I have the following piece of code in a WordPress site which has custom posts.
This appears in a functions.php file.
It was a purchased template and I need to count the just the PUBLISHED custom posts and i added this in the code below :
'.'('.$option->count.')'.'
It is working just fine at the moment, but it's counting trash as well.
Please could somebody help me, thank you very much.
function dox_get_list_terms( $taxonomy = 'category', $term_id, $number, $orderby = 'name', $order = 'ASC', $hide = '0' ) {
$terms = array();
$terms = explode(',', $term_id);
$count = count( $terms );
$output = '';
foreach( $terms as $term ) {
if ($term >= 0) {
$options = get_terms( $taxonomy, 'number='.$number.'child_of='.$term.'&parent='.$term.'&hide_empty='.$hide.'&hierarchical=1&depth=1&orderby='.$orderby.'&order='.$order );
if (! is_wp_error($options) ) {
foreach ($options as $option) {
$output .= '<li>
<a href="'.get_term_link($option->slug, $taxonomy).'">
'.$option->name.'
</a>
'.'('.$option->count.')'.'
</li>';
}
}
}
}
return $output;
}
If you set hide_empty=1 in get_terms() function, Then you`ll only get terms who are assigned to any published post or custom posts.
Terms with 0 count will be ignored.