WP_Query filter by dayofweek

Home Forums Calendar Products Events Calendar PRO WP_Query filter by dayofweek

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #972639
    Dev
    Participant

    Hello,

    I am building a custom query using WP_Query for The Events Calendar. I am trying to filter events by a specific day of the week e.g. Display ALL events that are on Mondays. Does anyone happen to know how I can achieve this?

    I understand there is a WP_Query date_query function, but this only appears to work for WordPress’s default post_date column.

    Also, if someone knows how to achieve this via tribes default tribe_get_events loop, then please let me know.

    Any help is much appreciated!

    Current PHP code:

    
    					$args = array(
    						//'post_type' => 'tribe_events',
    						'post_type'=>array(TribeEvents::POSTTYPE),
    						'meta_key'=>'_EventStartDate',
    						'orderby'=>'_EventStartDate',
    						'order'=>'ASC',
    						'posts_per_page' => 999,
    						'meta_query'  => array(
    						                  'key'     => '_EventStartDate',
    						                  'type'    => 'DATE'
    						                ),
    						'date_query' => array()
    					);
    

    Current Array looks like

    (
        [post_type] => Array
            (
                [0] => tribe_events
            )
    
        [meta_key] => _EventStartDate
        [orderby] => _EventStartDate
        [order] => ASC
        [posts_per_page] => 999
        [meta_query] => Array
            (
                [key] => _EventStartDate
                [type] => DATE
                [date_query] => Array
                    (
                        [0] => Array
                            (
                                [dayofweek] => Array
                                    (
                                        [0] => 6
                                        [1] => 7
                                    )
    
                                [compare] => =
                            )
    
                    )
    
            )
    
        [date_query] => Array
            (
            )
    
    )

    Thanks,

    Nick

    #972779
    Brook
    Participant

    Howdy Nick,

    I would love to help you with that. That’s a pretty in-depth thing to do, but you are already well on your way.

    The standard WP_Query does not have enough capability built-in to do this. However, it makes it easy to add capability to it. Checkout the posts_where filter. Using this filter you could add a custom parameter to WP_Query ( which will also automatically be added to tribe_get_events() since that function is a wrapper for WP_Query ). Let’s say this new Query parameter is called “tribe_dayofweek”. I recommend you checkout our own extension to posts_where, which can be found in:/the-events-calendar/src/Tribe/Query.php:514 Right there on line 522 you can see how we detect if someone has declared the additional Tribe parameter ‘start_date’, and as you read on you can see how we modify the where clause of this SQL query.

    In this case you are going to want to add a where parameter which implements SQL’s DAYOFWEEK function. If you’re like me the MySQL documentation is not always very helpful. But here’s a pretty comprehensive answer on stackoverflow on how to implement this function. It’s the same SQL function we use for our Filter Bar (which allows users to select which day(s) of the week they would like to see events for) and the same SQL WordPress uses for its date_query =>dayofweek magic.

    There you go! Let me know if you have any questions. Thanks for posting, Nick.

    – Brook

    #985048
    Support Droid
    Keymaster

    This topic has not been active for quite some time and will now be closed.

    If you still need assistance please simply open a new topic (linking to this one if necessary)
    and one of the team will be only too happy to help.

Viewing 3 posts - 1 through 3 (of 3 total)
  • The topic ‘WP_Query filter by dayofweek’ is closed to new replies.