WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Hi I'm trying to make a page template that shows a list of a custom post type ordered by a meta key, I easily made it with query_posts but I am trying to do it via pre_get_posts so I made a function like this:

add_action('pre_get_posts', 'events');
function events($query) {
  if ( is_page_template('page-myevents.php') ){
    $query->set('post_type', 'events');
    $query->set( 'meta_key', 'start' );
    $query->set( 'orderby', 'meta_value_num' );        
    $query->set( 'order', 'DESC' );
    $query->set('numberposts', 5); 

But it does not work when i enter the page it sends me to the 404 page, I even added $query->get_posts() but nothing I get errors with that, any suggestions?

share|improve this question
up vote 0 down vote accepted

I'm not sure this is possible. Why do you need a page template? Why can't you use a custom post type archive, which is built-in to WordPress ( if you register the post type with has_archive set to true). I'm ignoring all your other possible $args, because I don't know how you are registering this post type.

register_post_type( 'events', array( 'has_archive' => true ) );

Then you can just adjust the query to order by the meta

add_action('pre_get_posts', 'events');
function events($query) {
  if ( is_post_type_archive('events' ) ){
    add_query_var( 'meta_key', 'start' );
    add_query_var( 'orderby', 'meta_value_num' );        
    add_query_var( 'order', 'DESC' );
    add_query_var('numberposts', 5); 
share|improve this answer
Hey thanks for the answer. The thing is I want to make a list of posts that the user can edit, not necessary posts that are assigned to the user. It can be done using a meta_query which was very easy to do with WP_Query but when I tried with pre_get_posts I get a 404 error. Could probably change the author archive in case this is not possible. – Poxtron Mar 9 '13 at 18:36
@TolucaCulturalInvitación - you get a 404 error because WordPress is trying to load the requested pagename from your events post type. You can't reliably convert a query for a page into a different sort of query via pre_get_posts, you need to do a separate query via WP_Query. – Milo Mar 9 '13 at 19:12
@TolucaCulturalInvitación Ah ok. Didn't know you were trying to something other than list the posts. @Milo confirmed what I suspected about converting the query, so unless you can explain that part more clearly, I too would suggest WP_Query – helgatheviking Mar 9 '13 at 20:05
So if there is no way to convert a query, is there a way of not running the query? The case is, I don't really need the page contents so there is no point on making that query. – Poxtron Mar 12 '13 at 0:17

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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