简体   繁体   中英

WP_Query category parameter is not working

I am creating shortcode with attributes and all attributes working except event_category. When i add "event_category" attribute it is not giving result as per added category.

Here are my shortcode attributes

    // Shortcode Default Array
            $default_shortcode_attrs = array(
                'type' => 'upcoming',
                'search' => 'true',
                'event_category' => '',
                'events_limit' => '-1',
            );

    extract(shortcode_atts($default_shortcode_attrs, $attr));

Following are query parameters

     $args = array(
                'posts_per_page' => -1,
                'post_type' => 'event_listing',
                'post_status' => 'publish',
                'event_listing_category'=> $event_category,
            );
     $query = new WP_Query($args);

"event_listing_category" is name of custom taxonomy. Please guide me why this query is not fetching the events according to their category.

Any help will be appreciated.

Thanks

Use tax_query instead as below: I'm assuming you have only one category slug provided in $event_category. If you have more than one category slug in that variable then try converting it to array and replace the whole array($event_category) with the array.

$args = array(
            'posts_per_page' => -1,
            'post_type' => 'event_listing',
            'post_status' => 'publish',
            'tax_query' => array(
                                array(
                            'taxonomy' => 'event_listing_category', 
                            'field' => 'slug', 
                            'terms' => array( $event_category)
                            )
                        )
        );
 $query = new WP_Query($args);

See http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters on how to query for custom taxonomies within your query.

The WP_Query function doesn't understand 'event_listing_category' as an argument, you'll have to tell wordpress that it's a custom taxonomy you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM