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.
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 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);
I found this function to display the terms attached to a post but i can't manage to find a way to exclude specific ID's of some category terms i don't want to display in the list.
Could someone give me a clue were to start? I looked up all the functions used in this function but can't seem to find arguments to exclude id's.
Thanks in advance!
// get taxonomies terms links
function custom_taxonomies_terms_links() {
global $post, $post_id;
// 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);
$out = "<ul>";
foreach ($taxonomies as $taxonomy) {
// get the terms related to post
$terms = get_the_terms( $post->ID, $taxonomy );
if ( !empty( $terms ) ) {
foreach ( $terms as $term )
$out .= '<li>'.$term->name.'</li> ';
}
}
$out .= "</ul>";
return $out;
}
Add another conditional if statement within your second foreach() to check whether or not the $term should be ignored. For example:
// An array of IDs to ignore/exclude
$excluded_ids = array( 1, 2, 3, 4);
foreach ( $terms as $term ) {
// Only proceed if the term_id is NOT in the $excluded_ids array
if ( !in_array( $term->term_id, $excluded_ids ) ) {
$out .= '<li>'.$term->name.'</li> ';
}
}
I'm using WordPress Multisite and I'm trying to display all the categories in every site on one page. When I'm on my admin account, the following code works. However, when I switch to any other account, no categories are shown.
$t=get_current_blog_id();
foreach(function_that_gets_blogs() as $k=>$blog){
switch_to_blog($blog['blog_id']);
print_r(get_categories(array('hide_empty'=>true))); // prints "array()"
foreach(get_categories(array('hide_empty'=>true)) as $cat){
...
}
}
switch_to_blog($t);
Why aren't the categories showing?
Like b__ said you should check for:
Where are you using this code? (functions.php, plugin)
You should disable plugins 1 by 1, to check if some are interfering
and in the last case change theme
I've done something like you, and here is the code, in case you wanna try it:
// Current Site
$current = get_current_site();
// All Sites
$blogs = get_blog_list( 0, 'all' );
foreach ( $blogs as $blog ) {
// switch to the blog
switch_to_blog( $blog['blog_id'] );
// get_categories args
$args = array(
'hide_empty' => true
);
$categories = get_categories( $args );
foreach ( $categories as $category ) {
$link = get_category_link( $category->term_id );
$name = $category->name;
printf( '%s ', $link, $name, $name );
}
}
// return to the current site
switch_to_blog( $current->id );
Update 2014/06/01
the function get_blog_list(); is deprecated since version 3.0, with that you should change that function within wp_get_sites();
// Current Site
$current = get_current_site();
// All Sites
$blogs = wp_get_sites();
foreach ( $blogs as $blog ) {
// switch to the blog
switch_to_blog( $blog['blog_id'] );
// get_categories args
$args = array(
'hide_empty' => true
);
$categories = get_categories( $args );
foreach ( $categories as $category ) {
$link = get_category_link( $category->term_id );
$name = $category->name;
printf( '%s ', $link, $name, $name );
}
}
// return to the current site
switch_to_blog( $current->id );
Simple as that...
Did you try:
<?php wp_list_categories("title_li=");?>
function get_people_cats($taxonomy) {
$output = '';
$terms = get_terms($taxonomy);
$count = count($terms);
if ( $count > 0 ):
foreach ( $terms as $term ):
$output .= "'". $term->name ."'". '=>';
$output .= "'". $term->term_id."',";
endforeach;
endif;
return $output;
}
This function returns a list of custom taxonomies and which words are found if the function is called in a template. But I want to assign the the function values to a variable in functions.php, and it's returning nothing.
If your Taxonomies are actually creaetd with a plugin instead of creating them with code in your functions.php the result of get_terms will be empty untill the taxonomies have initiated, which is on plugin level initiation better yet, most probably using the 'init' hook, so you would have to hook your function after the hook and use it only in your theme template files (not in functions.php) basicly you do something like:
add_action('init', 'get_people_cats', 9999);
Then you would call it normaly: $cats = get_people_cats('person_category');
Hope this solves your issue (I know it took me around an hour to solve this when I ran into it).
If you're not getting terms back it's probably due to where the taxonomy is being registered. If it's being registered in an init hook then you're likely trying to print them out before the taxonomy has actually been registered.
You can test it with the following:
function get_people_cats( $taxonomy ) {
$output = '';
$terms = get_terms('category');
$count = count( $terms );
if ( $count > 0 ):
foreach ( $terms as $term ):
$output .= $term->name.'=>'.$term->term_id;
endforeach;
endif;
return $output;
}
function echo_cats() {
echo get_people_cats('taxonomy_name', array('hide_empty' => 0) );
}
add_action('wp_footer', 'echo_cats');
By hooking into wp_footer it's not being called until well after any plugins that might have registered the taxonomy.
// UPDATE //
Got it. To create an array just do this:
$terms = get_terms($taxonomy, array('hide_empty' => false) );
if( !is_wp_error( $terms ) ) {
foreach( $terms as $term ) {
$types[$term->term_id] = $term->name;
}
}
return $types;
}
That will give you an array of $term->id => $term->name -- you may want to reverse that depending on how you're using the array.
Try this, works fine in my functions.php file on a local site:
function get_people_cats( $taxonomy ) {
$output = '';
$terms = get_terms( $taxonomy );
$count = count( $terms );
if ( $count > 0 ):
foreach ( $terms as $term ):
$output .= $term->name.'=>'.$term->term_id;
endforeach;
endif;
return $output;
}
echo get_people_cats('category');