$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>plugins - category_name not working (not showing up in sql query debug)|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

plugins - category_name not working (not showing up in sql query debug)

matteradmin9PV0评论

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.

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.

Share Improve this question asked Feb 23, 2016 at 11:43 LazharLazhar 1834 bronze badges 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 – Pieter Goosen 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 ;-) – Pieter Goosen Commented Feb 23, 2016 at 13:02
Add a comment  | 

1 Answer 1

Reset to default 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.

Post a comment

comment list (0)

  1. No comments so far