Quantcast
Channel: WordPress › Support » Forum: Hacks - Recent Topics
Viewing all articles
Browse latest Browse all 8245

DatagoniaWeb on "Modifying the Main Loop Query (wp_query->set)"

$
0
0

Good Morning,

I am currently building a website that will be a blog about music and am implementing custom fields and custom post types in several different manners. On my archive-event.php page I specifically want to modify the main loop for that page so that instead of ordering by the date the post (each event is a post of type "event") was published I want them sorted in the archive by the date of a custom field called "start_date".

Currently each start_date field is in the form "yyyy-m->set-dd" and I want to omit posts with a start_date before the current date (so previous events would not show up). Additionally I would like to order the archive of the posts by start_date so that the first post would be the soonest upcoming event.

I was reading a little bit about the pre_get_posts function but was not able to implement it correctly. The two main areas of code for attempting to modify the query are in functions.php and archive-event.php:

Functions.php:

//specifically alter the main query *after* it has been called
add_action('pre_get_posts','alter_events_query');
function alter_events_query($query){
      if( $query->is_main_query() ){
        $query->set( array('meta_key' => 'start_date', 'meta_value' => date('o-m-d'), 'meta_compare' => '>=' ));
      }
}

Event-archive.php:
alter_events_query( );

It should be noted that the "alter_events_query();" line is called immediately after the "get_header" call at the beginning of archive-event.php.

My website is: RaveIsKing.Danconia.US and the page, including error message, is the following (debugging is currently on):
http://raveisking.danconia.us/event/

Any help that could point me in the right direction would be appreciated. I don't know if I am sending the right parameters to the wp_query->set method and it seems that documentation on that method and the "pre_get_posts" isn't very well documented either. Thank you!


Viewing all articles
Browse latest Browse all 8245

Trending Articles