Can ‘numberposts’ be passed in the URL query string?

Well, numberposts is not actually a query variable. It’s just turned into posts_per_page in get_posts() before the query is run. posts_per_page is a private query var, which means you can’t run it in the query string. A possible solution would be to register a custom query variable (let’s say 'latest_mealplan' and add that variable to the rewrite rule (e.g. index.php?post_type=mealplan&orderby=date&order=DESC&latest_mealplan=1).

Then, hook into 'parse_request', which passes the $wp object to the callback. From there, it’s just a matter of setting the parameter:

if( !empty( $wp->query_vars['latest_mealplan'] ) ){
  $wp->query_vars['posts_per_page'] = 1;
  add_filter( 'template_include', create_function( '$a', 'return locate_template(array("single-mealplan.php"));' ) );
}

Hope this helps!

Leave a Comment