Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get all records of taxonomy category in WordPress Query?

Tags:

php

wordpress

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');
like image 220
Nikunj Soni Avatar asked Oct 29 '14 10:10

Nikunj Soni


2 Answers

You have a lot of errors in your code and a misunderstanding about categories.

  • Never use query_posts to construct a custom query

Note: 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();
}
like image 110
Pieter Goosen Avatar answered Sep 16 '22 12:09

Pieter Goosen


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 } ?>
like image 42
bhavesh vala Avatar answered Sep 16 '22 12:09

bhavesh vala