5

My plugin has a shortcode and it retrieves animals. There are filters/arguments and they all work well except the category argument or parameter.

The category name(s) is/are well passed to my shortcode function but the WP_Query->request does not show any sign of category_name! I tried with cat and the ID, same, nothing shows up. Instead, it return an empty set of results.

    $filters    = array();
    $cat        = "";

    if( !empty($args['cat']) ) {
        $cat = trim($args['cat']);
    }

    $query = new WP_Query( array( 
        'post_type'         => 'animal',
        'category_name'     => 'rottweiler', /* ignored/breaks the sql query */
        'post_status'       => 'publish',
        'orderby'           => 'title',
        'order'             => 'asc',
        'numberposts'       => -1,
        'posts_per_page'    => -1,
        'meta_query'        => $filters
    ) );

    $animals = $query->get_posts();

    if( $animals ) {

        $columns = empty($args["columns"]) ? 2 : $args["columns"];

        $output .= '<div class="breedr"><ul class="columns '.number_to_words($columns).'">';

        foreach ( $animals as $animal ) {
            $output .= $this->breedr_template( plugin_dir_path( dirname( __FILE__ ) ) . 'public/partials/list-single-animal.php', $animal );
        }

        $output .= '</ul></div>';

    }
    else {
        $output .= "<span>No animal found.</span>";
    }

    return $output;

And this is what the debug sql returns - AND ( 0 = 1 ) is clearly posing problem here:

CAT: rottweiler
SELECT wp_2_posts.* FROM wp_2_posts WHERE 1=1 AND ( 0 = 1 ) AND wp_2_posts.post_type = 'animal' AND ((wp_2_posts.post_status = 'publish')) GROUP BY wp_2_posts.ID ORDER BY wp_2_posts.post_title ASC 

No animal found.

I am confused at what causes this problem. The category slug exists, every other parameter in this query works when I simply remove the category parameter.

3
  • Are you sure that you are using the build in taxonomy category and not a custom taxonomy. There is no reason why your query should not work Commented Feb 23, 2016 at 12:23
  • 2
    I am used a custom taxonomy that is linked to my custom post type.. oh, so i should do a tax query instead of category? argh.. silly of me.. will try this out! Thanks pieter..
    – Lazhar
    Commented Feb 23, 2016 at 12:25
  • 1
    Yes, custom taxonomies does not work with the category parameters, you should use a tax_query ;-) Commented Feb 23, 2016 at 13:02

1 Answer 1

1

If you're using custom taxonomy for your CPT, then you can't use params of WP_Query that are related with built-in categories - WP won't be able to guess what taxonomy should be queried.

So, let's say you have a custom taxonomy called animal_type, then your query should look something like this:

$query = new WP_Query( array( 
    'post_type'         => 'animal',
    'post_status'       => 'publish',
    'orderby'           => 'title',
    'order'             => 'asc',
    'posts_per_page'    => -1,
    'tax_query'         => array(
        array( 'taxonomy' => 'animal_type', 'field' => 'slug', 'terms' => 'rottweiler' )
    ),
    'meta_query'        => $filters
) );

Notice, that I've also deleted this part:

'numberposts'       => -1,

There is no such parameter for WP_Query. There was parameter called showposts, but it's deprecated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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