Have a custom-taxonomy called campaign_action with three categories called draft, live and paused.
I would like to display just the count for each but not in a list.
For example, I would like to echo the count for each individually as -
<li>Draft (<?php //code to display a number count for drafts ?>)</li>
<li>Live (<?php //code to display a number count for live ?>)</li>
<li>Paused (<?php //code to display a number count for paused ?>)</li>
I have successfully done this by displaying
foreach ( $terms as $term ) {
echo '(' . $term->count . ')';
}
However, this does not work for me and I want to get the $count for each individually.
Thank you for your help.
EDIT
To show further what I have in place currently
<?php
$terms = get_terms('campaign_action');
if ( !empty( $terms ) && !is_wp_error( $terms ) ){
echo '(0)';
foreach ( $terms as $term ) {
echo '(' . $term->count . ')';
}
}
?>
This will show all counts for each individual category but I only want to show the count for the category of draft within the custom_taxonomy of campaign_action
Here is an image of what the above achieves when added to the end of the Drafts. I want it to only show the count for the category of drafts within the custom-taxonomy of campaign_action. If it has zero posts with that category then it should show zero.
Try below code and read my comments of code.
echo wp_list_categories( array(
'orderby' => 'name',
'show_count' => true,
'taxonomy' => 'campaign_action' //i guess campaign_action is your taxonomy
) );
There is second way as well for custom html layout please check below code for custom html layout
$terms = get_terms(array(
'taxonomy' => 'campaign_action',//i guess campaign_action is your taxonomy
'hide_empty' => false
));
echo $terms->name;
echo $terms->count;
After Your question is edited :
$terms = get_terms(array(
'taxonomy' => 'campaign_action',//i guess campaign_action is your taxonomy
'hide_empty' => false
));
foreach ($terms as $terms)
{
if($terms->name == 'Draft')
{
echo $terms->name;
echo $terms->count;
}
}
You need to some arguments :
<?php
$args = array(
'post_type' => 'campaign_action',
'post_status' => 'publish' //(Or Draft...etc)
);
$show_recipes= get_posts( $args );
echo $show_recipes->post_count;
?>
Here is the full list of statuses in WP : https://codex.wordpress.org/Post_Status
Related
(first of all i'm new with php)
i have a wordpress blog where i have written about some ppl and i added their names as tags in a costum taxonomy to avoid multi language field with polylang.
I was looking for a loop that allow to show, for every tag, the title of post where tag exist.
Example => post's name: "what a chef can do?" (tagged Alex P.)
result: -Alex P. "what a chef can do?" 05/10/2020 - "Tomatos in the world" 15/12/2020.
For every person(tag) i have to make an Accordion.
i've started to write code but i'm not looking for a solution and i'm getting lost.
<?php $tag_args = array(
'orderby' => 'title',
'order' => 'ASC'
);
?>
<ul id="Genre-List">
<li>DEFAULT</li>
<?php
$terms = get_terms(
array(
'taxonomy' => 'chef',
'hide_empty' => false,
)
);
// Check if any term exists
if ( ! empty( $terms ) && is_array( $terms ) ) {
// Run a loop and print them all
print_r($terms);
foreach ( $terms as $term ) {
?>
<li><a href="<?php echo esc_url( get_term_link( $term ) ) ?>">
<?php echo $term->name; ?>
</a></li>
<?php
}
}
?>
Thanks if anyone can help about this.. im really getting mad.
If you are looking to get all posts which have any custom taxonomy term associated with it, you will need to query posts using a tax_query rather than simply retrieve all of the terms.
// get 'chef' taxonomy
$terms = get_terms([
'taxonomy' => 'chef',
'hide_empty' => false,
]);
// get any posts with one of the 'chef' taxonomy terms associated with it
$query = new WP_Query([
'post_type' => 'post',
'tax_query' => [[
'taxonomy' => 'chef',
'field' => 'term_id',
'terms' => array_column($terms, 'term_id'),
'operator' => 'IN',
]],
]);
// loop and output posts
while ($query->have_posts()) {
$query->the_post();
var_dump(get_the_title());
}
The code works perfectly for listing WooCommerce product tags, but I want to add a query with it.
I would like to list only product tags that contain a specific String.
<?php
$terms = get_terms(array(
'taxonomy' => 'product_tag',
'hide_empty' => false,
));
$count = count($terms);
echo "found ". $count . " Schools";
?>
<div class="product-tags">
<ul>
<?php foreach ( $terms as $term ) { ?>
<li><?php echo $term->name; ?></li>
<?php } ?>
</ul>
</div>
Use WP_Term_Query instead of get_terms
$keyword = 'tag';
// Args
$args = array(
'taxonomy' => 'product_tag',
'hide_empty' => false,
'name__like' => $keyword,
);
// Term Query
$query = new WP_Term_Query($args);
// Get terms
$get_terms = $query->get_terms();
// Count
$count = count( $get_terms );
echo "found ". $count . " Schools";
// Loop
foreach ( $get_terms as $terms ) {
echo $terms->name;
}
You can use the 'search' or 'name_like' fields in the first argument array, per the wordpress documentation here:
https://developer.wordpress.org/reference/classes/wp_term_query/__construct/
For example, say you want to get all terms where the name contains 'foo'
<?php
$terms = get_terms(array(
'taxonomy' => 'product_tag',
'hide_empty' => false,
'name__like' => '%foo%'
));
The following code I wrote is supposed to show all the product categories of a wordpress ecommerce website.
<?php $categories = get_terms(
array(
'taxonomy' => 'product_cat',
'hide_empty' => 'false',
'numberposts' => -1)
);
?>
<?php var_dump($categories); ?>
<?php foreach( $categories as $category ): ?>
<h4 class="shop-category-name d-inline"><?php echo $category->name; ?></h4>
<?php endforeach; ?>
I'm using it inside a woocommerce hook that is responsible to render the contents before the main shop page, the woocommerce_before_main_content.
I'm not able to get the categories, I will see only one category and the others are not listed. I'm not sure about, but maybe this can be something related to the fact that I'm using the function inside a woocommerce hook? I had a similar issue with the shop page featured image, I was not able to display it because of this motivation and I have modified the code to use the wc_get_page_ID('pag name').
Is there a fix ?
Try to use it like this in the function, you used for woocommerce hook woocommerce_before_main_content
add_action( 'woocommerce_before_main_content', 'woo_cats', 20, 0 );
function woo_cats(){
$cat_args = array(
'orderby' => 'name',
'order' => 'asc',
'hide_empty' => false,
);
$product_categories = get_terms( 'product_cat', $cat_args );
if( !empty($product_categories) ){
echo '<ul>';
foreach ($product_categories as $key => $category) {
echo '<li>';
echo '<a href="'.get_term_link($category).'" >';
echo $category->name;
echo '</a>';
echo '</li>';
}
echo '</ul>';
}
}
I'm having trouble displaying the loop category correctly. Need display only one category in lit posts. Why the first post gets all selected categories and the next posts already have the correct display of one category. How can I make only the current selected category displayed in the first post? My code looks like this. I attached the picture.
My loop custom post
<?php
/* Start the Loop */
$args = array( 'post_type' => 'database',
'posts_per_page' => 10,
'paged' => $paged,
'post_status' => 'publish',
'ignore_sticky_posts' => true,
'order' => 'DESC',
'orderby' => 'date');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
This is for display one category in loop post
<?php
// Get terms for post
$terms = wp_get_post_terms( $post->ID , 'database_categories' ,$args2);
$args2 = array( 'parent' => 39,
'fields' => 'all');
// Loop over each item since it's an array
if ( $terms != null ) {
foreach( $terms as $term ) {
$term_link = get_term_link( $term, 'database_categories' );
// Print the name and URL
echo '' . $term->name . ' ';
// Get rid of the other data stored in the object, since it's not needed
unset($term);
}
}
?>
If the object here is to get the first term and ignore the rest then I would just shift off the first array element and no need for a loop on terms.
$args2 = array('parent' => 39, 'fields' => 'all');
$terms = wp_get_post_terms( $post->ID , 'database_categories' ,$args2);
if (!empty($terms)) {
$term = array_shift($terms);
echo sprintf('%s', get_term_link( $term, 'database_categories' ), $term->name);
}
I am trying to display the names categories of a custom taxonomy called standard_engine_specification on a template page. This is what I have so far - it is showing all the categories of the taxonomy instead of just showing the ones checked in the admin panel.
<?php
/*variable to retrieve checked term*/
$ses_terms = get_the_terms( $id, 'standard_engine_specification' );
if( $ses_terms && !is_wp_error( $ses_terms ) ) {
foreach( $ses_terms as $term ) {
}
}
/*variable used to filter results*/
$ses_args = array(
'taxonomy' => 'standard_engine_specification',
'hierarchical' => true,
'tax_query' => array(array(
'taxonomy' => 'standard_engine_specification',
'field' => 'slug',
'terms' => array($term->slug),
'operator' => 'IN'
))
);
?>
<ul>
<?php
/*output checked categories based on filter*/
foreach (get_categories($ses_args) as $category)
{
echo "<li>";
echo $category->name;
echo "</li>";
}
?>
</ul>
I've pretty much Frankensteined this from some other scripts I have been using for filtering post types, maybe someone who knows what they are doing can tell me where and why I am missing this - I have put in some comments.
Ok sorted it. I did not need to use a tax_query, I tried to do the same thing I had done calling WooCommerce onto a template page but it was overkill, so we learn :)
<?php
$ses_terms = get_the_terms( $post->ID, 'standard_engine_specification' );
if( $ses_terms && !is_wp_error( $ses_terms ) ) {
foreach( $ses_terms as $term ) {
echo "<li>";
echo $term->name;
echo "</li>";
}
}
?>