简体   繁体   中英

WP_Query with more than one post_type

I am trying to query more than one post type. I can actually query more than one post type easily and paginate and all but, later i realize that i need to get only specific from one of the post type and now im having trouble achieving that.

Out of two post_type im using one is regular post and another is event .

I am using this plugin for event(s): https://wordpress.org/plugins/event-organiser/

Here is the code that i use to query More than two post type.

$args = array(
                'post_type' => array('post','event'),
                'posts_per_page' =>$posts_per_page,
                'paged' => $paged
            );
    $post = new WP_Query($args);

Above code works fine until i try go query where event is >= todays date (means only show active event which are happening today or in future)

please give me some suggestion(s) or help

Thanks

Update

Here is the new $args

$args = array(
                'post_type' => array('post','event'),
                'posts_per_page' =>$posts_per_page,
                'paged' => $paged,
//                'meta_key' => '_eventorganiser_schedule_start_finish',
                'meta_query' => array(
                    'relation' => 'OR',
                    array(
                        'key' => '_eventorganiser_schedule_start_finish',
                        'value' => date('Y-m-d'),
//                        'value' => date('Y-m-d h:i:s', time()),
                        'type' => 'NUMERIC',
                        'compare' => '>='
                    ),
                    array(
                        'key' => '_edit_lock',
                        'compare'  => 'EXISTS'
                    )
                )
            );

New $args Generate Following Query:

SELECT SQL_CALC_FOUND_ROWS multi_posts.ID FROM multi_posts INNER JOIN multi_postmeta ON ( multi_posts.ID = multi_postmeta.post_id ) INNER JOIN multi_postmeta AS mt1 ON ( multi_posts.ID = mt1.post_id ) WHERE 1=1 AND ( ( multi_postmeta.meta_key = '_eventorganiser_schedule_start_finish' AND CAST(multi_postmeta.meta_value AS SIGNED) >= '2020-05-05' ) OR mt1.meta_key = '_edit_lock' ) AND multi_posts.post_type IN ('post', 'event') AND (multi_posts.post_status = 'publish' OR multi_posts.post_status = 'acf-disabled') GROUP BY multi_posts.ID ORDER BY multi_posts.menu_order, multi_posts.post_date DESC LIMIT 0, 9

Well it still not filtering events which are expired.

I think your plugin also using a start and end date with meta field. You should check the database and use the meta query. that will help you to get the event that has started before today's date.

This is what i used to get my results.

$args = array(
                'post_type' => array('post','event'),
                'posts_per_page' =>$posts_per_page,
                'paged' => $paged,
                'suppress_filters'=>false,
                'meta_query' => array(
                    'relation' => 'OR',
                    array(
                        'key' => '_eventorganiser_schedule_start_finish',
                        'value' => date('Y-m-d h:i:s', time()),
                        'type' => 'DATETIME',
                        'compare' => '>=',
                    ),
                    array(
                        'key' => '_eventorganiser_schedule_start_finish',
                        'compare'  => 'NOT EXISTS'
                    )
                )
            );

I think since post doent have _eventorganiser_schedule_start_finish it works fine.

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