0

I have archive-events.php file that sorts custom post type 'events' via WP_Query using custom field that is date formatted YYYYMMDD (20150215) like this:

$args = array(
    'post_type'     => 'events',
    'posts_per_page'=> 10,
    'meta_key'      => 'wpcf-sort-events',
    'orderby'       => 'meta_value_num',
    'order'         => 'ASC'
);
$wp_query = new WP_Query( $args );

Then I made a widget that displays custom calendar with date links to compare custom field dates via add_query_arg and display events archived from that date onwards like this:

$day = I get the day for the calendar before this... 01
$date = I get the full date (Ymd 20150501) from calendar widget to compare
$arrayargs = array(
    'meta_query' => array(
         'key'        => 'wpcf-sort-events',
         'value'      => $date,
         'compare'    => '>='
         ),
     );                                                                                                                                                         

$output = '<a href="' . add_query_arg( $arrayargs ) . '>' . $day . '</a> ';

This creates a link like this: http://mediterranean.life/events/?meta_query%5Bkey%5D=wpcf-sort-events&meta_query%5Bvalue%5D=20150501&meta_query%5Bcompare%5D=%3E

But I get all events (no matter the custom date) sorted ASC like query in archive-events.php, it ignores meta_query via link...

I have tried creating a link with a full query like this, but also failed...

$arrayargs = array(
            'post_type' => 'events',
            'meta_key' => 'wpcf-sort-events',
            'meta_query' => array(
                'key' => 'wpcf-sort-events',
                'value' => $date,
                'compare' => '>='
                ),
            'orderby' => 'meta_value_num',
            'order' => 'ASC'
        );                                                                                                                                                          

        $output = '<a href="' . add_query_arg( $arrayargs ). '>' . $day . '</a> ';

Lost in space... any ideas?

1 Answer 1

1

As you can see in your custom WP_Query, you pass hardcoded aguments, you are not evaluating the arguments passed in the URL, so they does not affect to the result.

In your case, it seems that using a custom query for the archive template is the bad way. When you request the archive template, the events for the archive have been already queried and you are refusing the results and making a new secondary query. Instead, you should use pre_get_posts action hook to alter the main query before it is executed, so you get the events you want in the archive template without the need of a secondary template.

add_action( 'pre_get_posts', function( $query) {

   if(isset($query->query_vars['action']) && $query->query_vars['action'] == 'search'){

       //Check that is the main query, not secondary, that we are not in admin side
       //and check that $query is the for the events archive

       if($query->is_main_query() && !is_admin() && is_post_type_archive('events') ) {

           //Get the values passed in the URL and pass them to the query
           if( ! empty( $_GET['meta_query'] ) ) {
               //You may need some validation/sanitization in $_GET['meta_query'] befor use it
               $query->set( 'meta_query', $_GET['meta_query'] );
           }


       }

   }


} );
2
  • So what I should do is use functions.php file to do what you are suggesting? Check if meta_query exists and use that (link I create in calendar widget), and if not enter the custom query there to sort events archive (query I use in archive template)...
    – ronimarin
    Feb 15, 2015 at 12:25
  • Yes, use functions.php. You don't need any custom query in the template file; if you alter the query in pre_get_posts action hook you are already getting the posts you want avoiding a secondary query.
    – cybmeta
    Feb 15, 2015 at 12:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.