I want to count the number of posts in each taxonomy. The below code only works if there is on 2nd level taxonomy. Based on the image. It's returning BMW Post count 2 but for Mercedez, it should return Post Count 4. Adding Model 1 & Model 2 post count.
$term = get_queried_object();
$term_id = $term->term_id;
$taxonomy_name = $term->taxonomy;
$termchildren = get_terms($taxonomy_name,array('parent' => $term_id));
foreach ( $termchildren as $child ) { ?>
<h3>
<?php echo $child->name;?>
<span class="blog_bg_blue">Post Count: <?php echo $child->count; ?></span>
</h3>
<?php} ?>
Are you looking for something like this?
Script:
$args = array(
'orderby' => 'slug',
'parent' => 0
);
$categories = get_categories( $args );
foreach( $categories as $category ){
print $category->name.' - '.$category->category_count.'</br>';
}
Post arrangement in CMS:
Output:
Related
I am trying to pull a posts custom taxonomy into the page. The categories have subcategories and I am struggling to workout how to get the subcategories listed in hierachical way. Currently I am using the below which is showing the categories properly, but they are coming in all jumbled.
$terms = get_the_terms( $post_id, 'listing_category' );
if ( $terms && ! is_wp_error( $terms ) ) :
$categories = array();
foreach ( $terms as $term ) {
$categories[] = $term->name;
}
$categories_list = join( ", ", $categories ); ?>
<tr>
<td><strong>Categories: </strong></td>
<td><?php esc_html_e( $categories_list ) ?></td>
</tr>
<?php endif; ?>
I have attempted numerous options but they all seem to be showing all of the categories and subcategories within that custom taxonomy, not only the categories relating to this post.
Any help or guidance would greatly appreciated.
you should get parent taxonomy and then for every parent get their children
with a loop(foreach).
1- Find the parent taxonomies by this query:
$args = array (
'taxonomy' => 'listing_category',
'parent' => 0,
'hide_empty' => false
);
$allTaxParent = get_categories($args);
2- Now create a function for get children from parent:
function getChildren( $termId=0, $taxonomy='category' ) {
$children = get_categories(
[
'child_of' => $termId,
'taxonomy' => $taxonomy,
'hide_empty' => false
]
);
return ($children);
}
3- Now you have taxonomies in hierachical way:
foreach ( $allTaxParent as $tax ) {
$children = getChildren($tax->term_id, $tax->taxonomy);
echo $tax->name . '>';
foreach ($children as $child){
echo $child->name . '>';
}
echo '<br>';
}
I am trying to get all categories that are attached to a custom post type in Wordpress.
I have a custom post type named doc_pipeline and a custom taxonomy named tax_pipeline. I have been searching for hours on this and I can't put the right functions together to make this happen. To repeat I need to get a category name that is attached to the custom post type only if it is checked. Should be fairly simple, but I am quite lost.
$args = array(
'post_type' => 'doc_pipeline',
'taxonomy' => 'tax_pipeline',
'posts_per_page' => -1
);
$categories = get_categories( $args );
$posts = get_posts($args);
foreach($posts as $post) {
var_dump(is_object_in_term( $post->ID, 'tax_pipeline', 'doc_pipeline'));
}
To get the category name of the post by post id you should use like this.
you already have post ID so, you don't have to pass post type.
$cat_array = wp_get_post_terms($post->ID, 'tax_pipeline', array('fields'=>'names'));
and
// To Remove dublicate category from loop, try this.
$unique_terms = array(); // instead of $dupe = 0;
while ( $wp_query->have_posts() ) : $wp_query->the_post();
global $post;
//Ok, Then you have to use like this in your loop.
$cat_array = wp_get_post_terms( $post->ID, 'tax_pipeline', array( 'fields' => 'all' ) );
foreach( $cat_array as $term ) :
if( ! in_array( $term->term_id, $unique_terms ) ):
array_push( $unique_terms, $term->term_id );
echo $term->name;
endif;
endforeach;
endwhile;
How to get list of custom taxonomy using custom post type name.
let say there is one post type called "products" and in that there are list of categories(ie. shirt, tshirt, jeans etc.)
So I want that categories list using post type name "products".
you can get post list by taxonomy in taxonomy-{post_type}.php In this file by default gt list of post for specified taxonomy.
REF : Custom Post Type taxonomy page still showing all posts
OR in Custom templete,
$custom_terms = get_terms('custom_taxonomy');
foreach($custom_terms as $custom_term) {
wp_reset_query();
$args = array('post_type' => 'custom_post_type',
'tax_query' => array(
array(
'taxonomy' => 'custom_taxonomy',
'field' => 'slug',
'terms' => $custom_term->slug,
),
),
);
$loop = new WP_Query($args);
if($loop->have_posts()) {
echo '<h2>'.$custom_term->name.'</h2>';
while($loop->have_posts()) : $loop->the_post();
echo ''.get_the_title().'<br>';
endwhile;
}
}
You could use something like this:
$terms = get_terms( 'products' );
$count = count( $terms );
if ( $count > 0 ) {
echo '<h3>Total products: '. $count . '</h3>';
echo '<ul>';
foreach ( $terms as $term ) {
echo '<li>'.$term->name.'</li>';
}
echo '</ul>';
}
get_term_link() will give you a link to your term as well.
With or without posts within. I need to display the total number of categories in my Wordpress theme.
Easy way to count category is:
1) firstly fetch all category from wordpress
2) count them using simple php funciton
complete code will be like:
<?php $args = array( 'parent' => 0, 'hide_empty' => 0 );
$categories = get_categories( $args );
echo "Total categories : ".count( $categories ); ?>
i used this code always :)
<?php
$args = array(
'get' => 'all',
'hide_empty' => 0
);
$categories = get_categories( $args );
echo count( $categories );
?>
A more straight forward way (perhaps faster?)
global $wpdb;
$categories_count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->terms" );
echo "<p>Categories count is {$categories_count}</p>";
Retrieve list of category objects get_categories():
<ul class="list-group">
<?php
$categories = get_categories();
$i = 0;
foreach ($categories as $category) {
$i++;
echo '<li class="list-group-item"><span class="text-muted float-left">' . $category->category_count . '</span>' . $category->name . '</li>';
}
?>
</ul>
I am fighting to get what should be a very simple Wordpress custom taxonomy archive template to work.
I'm getting all the information and the posts are listed alphabetically but the terms are in order of id and need them in alphabetical order.
I'm fudging my way through this and am currently using the code from this post. I have tried a multitude of solutions from around the net, with no luck. I see this post has a solution but don't know how to implement it in the code below.
Perhaps there's an easier way of doing what I need?
The query needs to get the current parent term, then the child terms, and the posts in the child terms. The following code is on my taxonomy-business-categories-(parent term).php, for example my taxonomy-business-categories-bars.php. I need to output the child terms grouped with their posts. All must be in alphabetical order.
<?php
$term_slug = get_query_var( 'term' );
$taxonomyName = get_query_var( 'taxonomy' );
$current_term = get_term_by( 'slug', $term_slug, $taxonomyName );
$termchildren = get_term_children( $current_term->term_id, $taxonomyName );
foreach ($termchildren as $child) {
$term = get_term_by( 'id', $child, $taxonomyName );
$wpq = array (
'taxonomy'=>$taxonomyName,
'term'=>$term->slug,
'order'=>'asc',
'orderby'=>'title');
$query = new WP_Query ($wpq);
echo "$term->name:<br />";
?>
<?php
if ($query->have_posts() ) : while ($query->have_posts() ) : $query->the_post(); ?>
<?php the_title();?>,
<?php endwhile; endif; wp_reset_query(); ?>
<?php
echo "<br />";
}
?>
Here's a link to the taxonomy template as .txt. file.
UPDATE: As I'm hardcoding the taxonomy templates by parent term, could I use something like this with my code above:
<?php
$term_id = 32;
$taxonomy_name = 'business-categories';
$termchildren = get_term_children( $term_id, $taxonomy_name );
$children = array();
foreach ($termchildren as $child) {
$term = get_term_by( 'id', $child, $taxonomy_name );
$children[$term->name] = $term;
}
ksort($children);
Replace this code with your above provided code
<?php
$term_slug = get_query_var( 'term' );
$taxonomyName = get_query_var( 'taxonomy' );
$current_term = get_term_by( 'slug', $term_slug, $taxonomyName );
$termchildren = get_term_children( $current_term->term_id, $taxonomyName );
foreach ($termchildren as $child) {
$term = get_term_by( 'name', $child, $taxonomyName );
$wpq = array (
'taxonomy'=>$taxonomyName,
'term'=>$term->slug,
'order'=>'asc',
'orderby'=>'title');
$query = new WP_Query ($wpq);
echo "$term->name:<br />";
?>
<?php
if ($query->have_posts() ) : while ($query->have_posts() ) : $query->the_post(); ?>
<?php the_title();?>,
<?php endwhile; endif; wp_reset_query(); ?>
<?php
echo "<br />";
}
?>
Let me know if that works for you ...
The solution in my case was to install the plugin Custom Taxonomy Order NE and (as directed by the plugin's author) adjusted the query as follows:
Replace this:
$termchildren = get_term_children( $current_term->term_id, $taxonomyName );
foreach ($termchildren as $child) {
$term = get_term_by( 'id', $child, $taxonomyName );
$wpq = array (
With this
$termchildren = get_terms( array(
'taxonomy' => $taxonomyName,
'child_of' => $current_term->term_id,
) );
foreach ($termchildren as $term) {
$wpq = array (