I have use easy content Types plugin & created Post type in WP call Recipes. I have also added a taxonomy category in it & created 4 categories like Starter, Drinks, etc etc.
Now in WP query I need to get all records of starter. So how can I get that?
I am using this query, but it is not working. It is giving all records of Recipes post type Here is Query
$recipes = query_posts('post_type=recipes&taxonomy=recipescategory&category_name=Starters');
You have a lot of errors in your code and a misunderstanding about categories.
query_posts
to construct a custom queryNote: This function isn't meant to be used by plugins or themes. As explained later, there are better, more performant options to alter the main query. query_posts() is overly simplistic and problematic way to modify main query of a page by replacing it with new instance of the query. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination)
If you have to run a custom query, make use of WP_Query
or get_posts
category_name
takes the category slug, not the name. The parameter's name is deceiving
The "categories" belonging to a custom taxonomy are called terms. I have written a post which I have also included in the codex which you can check here, it describes the differences.
To retrieve posts from a custom taxonomy, you need to make use of a tax_query
. The category parameters will not work here
With all the above said, create your query so that it looks like this
$args = array(
'post_type' => 'recipes',
'tax_query' => array(
array(
'taxonomy' => 'recipescategory',
'field' => 'name',
'terms' => 'Starters',
),
),
);
$query = new WP_Query( $args );
if( $query->have_posts() ){
while( $query->have_posts() ) {
$query->the_post();
//Your loop elements
}
wp_reset_postdata();
}
try it
$ar = array (
'post_type'=>'recipes',
'taxonomy'=>'recipescategory',
'category_name'=>'Starters'
);
$posts = get_posts($ar);
** foreach loop**
foreach($posts as $p){ ?>
<div class="sub_cont">
<div class="sub_img">
<?php $url = wp_get_attachment_url( get_post_thumbnail_id($p->ID));?>
<a href="<?php echo $permalink = get_permalink( $p->ID); ?>"><img src="<?php echo $url; ?>" longdesc="URL_2" alt="Text_2" /> </a>
</div>
<div class="desc_title">
<a href="<?php echo $permalink = get_permalink( $p->ID); ?>">
<?php echo $post_title=$p->post_title; ?>
</a>
</div>
<div class="cont_add"></div>
</div>
<?php } ?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With