I'm trying to get the items from custom taxonomy on a page but not really going well. I would like to get the term name of custom taxonomy and custom field from custom post to show on the page. If there is no applicable item, would like to get the item from parent.
For example, As a category of food type,
**“Food> Italian> Pizza" (page category)**
On the other hand, in Recipes of custom post, If there was only
**"Food> Italian" (custom taxonomy is recipe-cat)**
The food of Pizza page will list recipe in the “Italian" category.
page category slug and taxonomy term are the same.
I appreciate your help!
$cat = get_the_category();
$children = get_term_children($cat->cat_ID ,'category');
$parents_cat = get_category(!($children));
if(!($children)) :
$args = array(
'post_type' => 'recipe',
'posts_per_page' => '5',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'recipe-cat',
'field' => 'slug',
'terms' => $parents_cat->slug,
)
),
);
$recipe = new WP_Query( $args );
if($parents_cat): ?>
<div class="recipe-box">
<h2><span><?php $parents_cat->name; ?>s recipe</span></h2>
<ul class="recipe-info">
<?php while ( $recipe->have_posts() ) : $recipe->the_post(); ?>
<?php $title = get_field('title'); if( $title ): ?>
<li><?php echo $title; ?></li>
<?php endif; ?>
<?php endwhile; ?>
</ul>
</div>
<?php endif; ?>
<?php endif; ?>
Related
I create two custom post_type. The name of the custom post is-
1. Brand
2. Ethical
And I have created a taxonomy. The name of the taxonomy is - Pharma. The common taxonomy of the two custom posts is one (pharma).
Now I want, on one page -
1. Just to display all the names of Pharma Taxonomy.
2. I would like to display only brand custom posts under Pharma Taxonomy.
3. I would like to count only the post of brand custom post under pharma taxonomy.
All right. But when I just call the brand custom post_type with Pharma Taxonomy then the ethic custom post also becomes a call. I want a solution.
$args = array(
'post_type' => 'brand',
'taxonomy' => 'pharma',
'order' => 'ASC',
'posts_per_page' => -1,
);
$query = new WP_Term_Query($args);
foreach ($query->get_terms() as $term) : ?>
<div class="item_wrap">
<h2><?php echo $term->name; ?></h2>
<span class="count"><?php echo $term->count; ?> <?php _e('brands'); ?></span>
</div>
<?php endforeach;
WP_Term_Query does NOT take 'post_type' argument Here's a list of valid argumentsDocs. However, you could add a WP_QueryDocs inside your foreach statement. Like this:
$pharm_args = array(
'taxonomy' => 'pharma',
'orderby' => 'name',
'order' => 'ASC',
);
$pharm_query = new WP_Term_Query($pharm_args);
foreach ($pharm_query->get_terms() as $term) :
$count_args = array(
'post_type' => 'brand',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => $term->taxonomy,
'field' => 'slug',
'terms' => $term->slug,
),
)
);
$count_term_in_cpt = new WP_Query($count_args);
if ($count_term_in_cpt->found_posts) {
?>
<div class='item_wrap'>
<h2><?php echo $term->name; ?></h2>
<span class='count'><?php echo $count_term_in_cpt->found_posts; _e('brands'); ?></span>
</div>
<?php
}
wp_reset_postdata();
endforeach;
Which will output this:
I have created a post_type "collection" and all its taxonomy is connected to post_type "product"
so when I do this
global $wp;
$posts = get_terms( $wp->query_vars["name"]);
foreach($posts as $post): ?>
<?=$post->name . " (".$post->count.")" ?>
<?php endforeach;
this will show all the terms in post_type "product" under the taxonomy "style"
but it also shows terms under the post_type "collection" under the taxonomy "style"
How can I exclude showing terms under post_type "collection"
Francis, the code currently fetches 'terms' not posts, which is why you are getting more than you want. If you looking to fetch posts (not terms) then use https://developer.wordpress.org/reference/functions/get_posts/.
This example is closest to what you are looking for https://developer.wordpress.org/reference/functions/get_posts/#comment-2516, substitute in your post_type and taxonomy.
I Kinda did it like this
<?php
$posts = get_terms($wp->query_vars["name"]);
$terms = get_terms($wp->query_vars["name"], array(
'hide_empty' => 0,
));
foreach( $terms as $term ) :
wp_reset_query();
$args = array('post_type' => 'product',
'tax_query' => array(
array(
'taxonomy' => $wp->query_vars["name"],
'field' => 'slug',
'terms' => $term->slug
),
),
);
$posts = new WP_Query($args);
if( $posts->have_posts() ) : ?>
<h3><?php echo $term->name; ?> - <?php echo $posts->post_count ?></h3>
<?php endif; endforeach; ?>
so the $posts->post_count did the trick
another is issue is that it hides when its empty, I need to show it all even empty ones
I am creating a FAQ page using a custom post type and custom taxonomy. I'm trying to create an unordered list for each taxonomy in order to group the FAQ's. In that unordered list, I want the first listed item to be the taxonomy name and then repeat the second listed item for all the questions in the taxonomy. This is the page I'm working on link.
It's currently duplicating the posts instead of displaying in the rightful taxonomies.
<?php
// get all the categories from the database
$cats = get_terms( array(
'taxonomy' => 'faq_categories',
));
// loop through the categories
foreach ($cats as $cat) {
// setup the category ID
$cat_id = $cat->term_id;
?>
<!-- Make a header for the category -->
<ul id="<?php echo $cat->slug; ?>" class="cd-faq-group">
<li class="cd-faq-title">
<h2>Questions <?php echo $cat->name; ?></h2>
</li>
<?php
// create a custom wordpress query
query_posts( array(
'post_type' => 'faqs',
'tax_query' => array(
array(
'taxonomy' => 'faq_categories', //or tag or custom taxonomy
'field' => 'slug',
'terms' => 'for-women'
)
)
));
// start the wordpress loop!
if (have_posts()) : while (have_posts()) : the_post(); ?>
<li>
<a class="cd-faq-trigger" href="#0"><?php the_title(); ?></a>
<div class="cd-faq-content">
<?php the_content(); ?>
</div>
</li>
<?php endwhile; endif; // done our wordpress loop. Will start again for each category
wp_reset_postdata();
?>
</ul>
<?php } // done the foreach statement ?>
Your query is not changing as you iterate through the $cats array. Perhaps changing the the value of the 'terms' array to $cat->slug would give you better results.
Thank you so much. You have both provided great insight as to what I was missing. I've resolved it now and this is how I went about solving it taking your suggestions into consideration.
<?php
$cats = get_terms(
array(
'taxonomy' => 'faq_categories',
'orderby' => 'term_id',
'order' => 'ASC'
)
);
foreach ($cats as $cat) :
?>
<ul id="<?php echo $cat->slug; ?>" class="cd-faq-group">
<li class="cd-faq-title">
<h2>Questions <?php echo $cat->name; ?></h2>
</li>
<?php
$questions = new WP_Query(
array(
'category_name' => $cat->slug
)
);
$questions = new WP_Query( array(
'post_type' => 'faqs',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'faq_categories',
'field' => 'slug',
'terms' => array($cat->slug),
)
)
));
?>
<?php if ($questions->have_posts()) : while ($questions->have_posts()) : $questions->the_post();?>
<li>
<a class="cd-faq-trigger" href="#0"><?php the_title(); ?></a>
<div class="cd-faq-content">
<?php the_content(); ?>
</div>
</li>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
In your query_post, your tax_query's field should be term_id and your terms be assigned to your $cat_id variable, instead of a hardcoded term.
How to display all categories of a custom post type on home screen without listing the items.
I already created the custom post type and it's categories, now I need to display all the categories on my home page as links to the each category page. Can someone help please?
You can use now get_categories
Here is an example of code:
<?php
$args = array(
'taxonomy' => 'Your Taxonomy Name',
'hide_empty' => 0,
'orderby' => 'name'
);
$cats = get_categories($args);
foreach($cats as $cat) {
?>
<a href="<?php echo get_category_link($cat->slug); ?>">
<?php echo $cat->name; ?>
</a>
<?php
}
?>
Remember write your taxonomy name as you registered, in here 'Your Taxonomy Name'
e.g. product_cat, blog_cat etc
Hope this will help you.
$cat_args = array(
'taxonomy' => 'your-custom-post', // your custom post type
);
$custom_terms = get_categories($cat_args);
echo print_r($custom_terms);
<?php
$terms = get_terms( 'taxonamy_name', array(
'orderby' => 'count',
'hide_empty' => 0
) );
foreach($terms as $term)
{
echo $term->name;
}?>
</ul>
</div>
$args = array( 'post_type' => 'post', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
the_title();
echo '<div class="entry-content">';
the_content();
echo '</div>';
endwhile;
i have a custom post type called 'electronics' with taxonomy categories called 'computers, phones, notebooks...and more'
in the taxonomy.php template, ive managed to get the taxonomy name, slug and id with this code.
<?php
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
echo $term->name; // will show the name
echo $term->slug; // will show the slug
echo $term->term_id; // will show the id
?>
so if im in the computers taxonomy page, i get 'Computers computers 11'
with either pf the values generated each time how can i generate the posts according to which 'taxonomy page' im in. eg. posts tagged with 'computers' in the computers taxonomy page, posts tagged with phones in the phone taxonomy page.. and so on.
like something likes this but for taxonomies...
<?php
$catquery = new WP_Query( 'cat=3' );
while($catquery->have_posts()) : $catquery->the_post();
?>
<ul>
<li><?php the_title(); ?>
<ul><li><?php the_content(); ?></li>
</ul>
</li>
</ul>
<?php endwhile; ?>
You need to use tax_query to get posts that have specific taxonomy term, here is how:
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$args = array(
'tax_query' => array(
array(
'taxonomy' => 'electronics',
'field' => 'slug',
'terms' => $term->slug
)
)
);
$catquery = get_posts( $args );
while($catquery->have_posts()) : $catquery->the_post();
?>
<ul>
<li><?php the_title(); ?>
<ul><li><?php the_content(); ?></li>
</ul>
</li>
</ul>
<?php endwhile; ?>