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.
category
and not a custom taxonomy. There is no reason why your query should not worktax_query
;-)