1

I've followed several related solution. But I'm still facing the problem. I've applied the following code for my custom post query by taxonomy (category). But it's not working. I think my problem with 'terms'.

$terms = get_terms('service_cat');
$services_post = new WP_Query(array(
    'post_type'         => 'service',
    'posts_per_page'    => -1,
    'tax_query' => array(
        array(
            'taxonomy' => 'service_cat',
            'field' => 'cat1',
            'terms' => $terms
        )
    ),
));

I also tried by the following code

$services_post = new WP_Query(array(
            'post_type'         => 'service',
            'posts_per_page'    => -1,
            'category_name'     => 'cat1' 
))
1

Edited to reflect question in OP's comment.

The contents of the tax_query array are not exactly right: as you're querying by slug, the field parameter of the individual taxonomy query should be "slug".

You're currently basically querying for all service posts from all service categories (well, not even that, because get_terms returns objects, but still).

To query for a single (or more than one) specific terms from which to load posts, specify the terms in the terms array. tax_query is then parsed accordingly.

$services_post = new WP_Query( array(
    'post_type'         => 'service',
    'posts_per_page'    => -1,
    'tax_query' => array(
        array(
            'taxonomy' => 'service_cat',
            'field' => 'slug',
            'terms' => array( 'cat1' ) // Array of service categories you wish to retrieve posts from
        )
    ),
) );

By the way, I am assuming you want to retrieve all posts of post type service

  • Your code only showing posts that have at least one service category. But I want to show posts by specific category slug. Can you help me about that please? – Md Jwel Miah Mar 31 '16 at 10:27
  • I've updated my answer to reflect the information from your comment. – engelen Mar 31 '16 at 10:31

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.