1

I have custom post type 'mix'. And custom taxonomy 'meal'. And categories in custom taxonomy (one of that is with slug: breakfast)

I have problems with making query. My code is :

            $args       = array(
                'post_type'         => 'mix',
                'tax_query' => array(
                    array(
                        'taxonomy' => 'meal',
                        'field' => 'breakfast'
                    )
                ),
                'posts_per_page'    => 50
            );
            $query      = new WP_Query( $args );

My loop is:

           if( $query->have_posts() ):
                while( $query->have_posts() ): $query->the_post();
                     //something here...
                endwhile;
           endif;

But problem is that nothing show up.

  • You said your taxonomy is 'obrok', but then in your query $args, you have 'taxonomy' => 'meal' Which is it? – frogg3862 Apr 30 '16 at 17:00
  • @frogg3862 my bad, i change post, see now, still not working – shapic94 Apr 30 '16 at 17:02
  • Try this: 'taxonomy' => 'meal', 'field' => 'slug', 'terms' => 'breakfast', – frogg3862 Apr 30 '16 at 17:02
  • @frogg3862 It works! But can you explane me why field is slug? – shapic94 Apr 30 '16 at 17:05
  • 1
    So the taxonomy and terms are obvious, but why does field have to be slug? When you add taxonomy parameters, you can specify what 'breakfast' is-- via the field. It could be the term's ID, the full name, or slug. See the Codex: codex.wordpress.org/Class_Reference/… – frogg3862 Apr 30 '16 at 17:09
2

The solution is to use:

'taxonomy' => 'meal', 
'field' => 'slug',
'terms' => 'breakfast'

The taxonomy and terms are obvious, but why does field have to be slug? When you add taxonomy parameters, you can specify what 'breakfast' is-- via the field. It could be the term's ID, the full name, or slug. See the codex on Taxonomy Parameters

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.