Only show checked categories of Custom Taxonomy - php

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>";
}
}
?>

Related

Wordpress get post from tags

(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());
}

WordPress get_terms() function not display custom taxonomy categories for woocommerce

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>';
}
}

Returning custom taxonomy in slug order

I have a loop to pull in custom taxonomy terms that I'd like to order by slug, but when I add in the 'orderby' command my loop is broken and returns nothing. No doubt I'm using incorrect syntax, but I can't see where for the life of me!
<?php
$loop = new WP_Query(
array(
'post_type' => 'camp',
'orderby' => 'name',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'types',
'field' => 'slug',
'terms' => $term->slug,
),
)
)
);
if ($loop->have_posts()) :
while ($loop->have_posts()) : $loop->the_post();
$terms = get_lowest_taxonomy_terms(get_the_terms( get_the_ID(), array(
'taxonomy' => 'destinations',
'orderby' => 'slug',
'order' => 'ASC'
)) );
?>
Any help would be very much appreciated :)
Added additional function:
The get_lowest_taxonomy_terms is running as follows:
get_lowest_taxonomy_terms( $terms ) {
// if terms is not array or its empty don't proceed
if ( ! is_array( $terms ) || empty( $terms ) ) {
return false;
}
$filter = function($terms) use (&$filter) {
$return_terms = array();
$term_ids = array();
foreach ($terms as $t){
if( $t->parent == 0 ) {
$term_ids[] = $t->term_id;
}
}
foreach ( $terms as $t ) {
if( $t->parent == false || !in_array($t->parent,$term_ids) ) {
//remove this term
}
else{
$return_terms[] = $t;
}
}
if( count($return_terms) ){
return $filter($return_terms);
}
else {
return $terms;
}
};
return $filter($terms);
}
Desired Output
The loop should query the taxonomy type 'destination', where that destination has the additional taxonomy of 'type'.
Screen grab of the current output:
The results are ordered into 3 cols by CSS, but as you can see, not sorting alphabetically.
get_the_terms doesn't take an array as second parameter.
get_the_terms( int|object $post, string $taxonomy )
Parameters:
$post (int|object) (Required) Post ID or object.
$taxonomy (string) (Required) Taxonomy name.
More details of get_the_terms
By default, get_the_terms doesn't have any sorting option. But, you can use usort to sort the returned array.
So, your last part of the code should be:
$terms = get_lowest_taxonomy_terms(get_the_terms( get_the_ID(), 'destinations' ));
This is a quick example of how you can sort it using usort:
$terms = get_the_terms( get_the_ID(), 'destinations' );
usort($terms,"so980_sort_terms_alphabetically");
function so980_sort_terms_alphabetically($a,$b) {
return $a['slug'] > $b['slug'];//using 'slug' as sorting order
}
so980_sort_terms_alphabetically function should be in your theme's functions.php file.
So finally, your last part will become:
$terms = get_the_terms( get_the_ID(), 'destinations' );
usort($terms,"so980_sort_terms_alphabetically");
$terms = get_lowest_taxonomy_terms($terms);
Update
I have just tested it, and it is returning a fatal error, as I have made a mistake. Error message: Fatal error: Cannot use object of type WP_Term as array.
get_the_terms returns an object. So, using $a['slug'] was causing the error.
Here, so980_sort_terms_alphabetically should be implemented in following way:
function so980_sort_terms_alphabetically($a,$b) {
return $a->slug > $b->slug;//using 'slug' as sorting order
}
**This is tested, and working.
Update 2
Since your output codes are not available in your question, I will assume that you are printing inside the loop. Instead of doing that, we will save returned terms in another array, and sort it outside the loop, so that we can get all of the terms at once.
Here is a quick example of how you can do it:
if ($loop->have_posts()) :
while ($loop->have_posts()) : $loop->the_post();
$pre_terms = get_lowest_taxonomy_terms(get_the_terms(get_the_ID(), 'product_tag'));
if ( $pre_terms ) {
foreach ( $pre_terms as $pre_term ) {
$terms[] = $pre_term;
}
}
endwhile;
endif;
usort($terms,"so980_sort_terms_alphabetically");
//a quick test to see if it's working or not
foreach ( $terms as $term ) {
echo $term->slug;
echo '<br/>';
}

How to get_terms $count for custom-taxonomy

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

Retrieve posts by custom taxonomy child on parent taxonomy page

So from the parent taxonomy view:
Child 1
post
post
post
Child 2
post
post
post
post
Child 3
post
post
post
post
I've scoured the internet for a solution to this, but nothing seems to be working. I'm able to successfully echo the term ID, but when I pass that into the query it returns nothing.
<?php
$terms = get_terms( 'ht_kb_category' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
echo '<ul>';
foreach ( $terms as $term ) {
if ($term->parent != 0) {
echo '<li><h1>' . $term->name . '</h1></li>';
$the_query = new WP_Query( array(
'post_type' => 'post',
'tax_query' => array(
array (
'taxonomy' => 'ht_kb_category',
'field' => 'slug',
'terms' => $term->slug,
)
),
) );
while ( $the_query->have_posts() ) :
echo '<p>'. the_title() .'</p>';
endwhile;
/* Restore original Post Data
* NB: Because we are using new WP_Query we aren't stomping on the
* original $wp_query and it does not need to be reset.
*/
wp_reset_postdata();
}
}
echo '</ul>';
} ?>
Finally found the solution after some detective work using the custom taxonomy URL. I was missing the 'post_type' and needed to query 'tag_id' instead of 'term_id'.
<?php $current_term_id = $hkb_current_term_id ?>
<?php
$myposts = get_posts(array(
'showposts' => -1,
'post_type' => 'ht_kb',
'tax_query' => array(
array(
'taxonomy' => 'ht_kb_category',
'field' => 'tag_id',
'terms' => $current_term_id)
))
);
echo '<ul>';
foreach ($myposts as $mypost) { ?>
<li><?php echo $mypost->post_title ?></li>
<?php }
echo '</ul>';
?>

Categories