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);
}
}
Related Posts:
- Changing the meta_query of the main query based on custom query_vars and using pre_get_posts
- How to uniquely identify queries?
- pre_get_posts with get_posts
- Can not switch the queried post in pre_get_posts hook
- Splitting the main query in multiple loops with query_posts and/or pre_get_posts?
- Using WP_Query in “parse_query” or “pre_get_post” in Posts2Posts
- pre_get_posts – Trying to get property of non-object warning
- `offset` WP_Query argument dont work via `pre_get_posts`
- pre_user_query vs pre_get_posts
- Using the same WP_Query for shop and widgets in WooCommerce shop
- wp_query – Modify $query to include duplicate content
- Pre_get_post on CPT archive page
- Search query alteration not working for meta values
- order by numeric value for meta value
- WP_Query orderby post__in remains ineffective in the Loop [closed]
- WP_Comment_Query pagination, delving into the unknown
- Sort posts by category name and title
- SQL Statement generated by WP_Query not producing expected results
- Highlighting Sub topic in a post?
- Sorting Posts by custom field
- how to retrieve specific product attribute value in an sql query?
- WordPress custom archive page
- WP_query category__in not working, only pulls from first category
- Retrieve or Query Pages by ID
- Searching through different categories on different pages code is not working
- How to implement time filter to show random post 1 month for one category and 3 months for other categories
- How to do a query on custom taxonomies that is uncategorised?
- Empty tax_query array returns an empty array
- How do I search inside specific taxonomies in WordPress
- Different string for specifed post type on posts listing at homepage
- if/else on custom query gives 200 OK when condition not met?
- How to correctly pass values to wpdb->prepare()?
- Single meta_query query using OR instead of AND in request’s WHERE statement
- Setting get_queried_object
- How to choose between hooking into per_get_posts or into parse_query
- remove query arg from url after set query
- difference between like ‘%%%var%%’ and ‘%var%’
- Store sticky post’s ids in a transient
- new WP_Query to get max price meta value not working
- WP_Query, pre_get_posts and offset
- How to detect if query is runned from widget?
- Using meta_query with multiple keys and compare values
- Can I set my WP_Query to be a Main Query?
- Sort query by author: 1 author, then others
- Overwrite YoastSEO meta-tags with another page’s [closed]
- How to orderby multiple meta fields if some fields are empty
- How to duplicate 5 posts out of WP_Query results?
- WP_query sorting can’t sort danish letters (æ, ø, å)
- Slow queries on a huge database
- Multisite pagination issue by multi query archive 404
- Query with relation and one without relation using multiple taxonomies?
- Is there a reason why Pages are not publicly_queryable?
- Shouldn’t I be able to modify the main query by this filter?
- Check return value of get_posts
- How Can I Change The Tax Query For The Main Loop For Taxonomy Archives?
- Exclude some authors from query
- using pre_get_posts for search results not found
- Hide elements outside loop based on query
- Transient api Caches confused
- How can I override one post and make it display content for another post?
- Get multiple users with meta value in one query and populate WP_User class
- Get a list of posts by specific category
- How to add custom meta to ‘pre_get_terms’?
- Shortcode for custom query not returning results when attributes are added
- Should I reset $wp_query?
- Sorting search results with custom dropdown
- Get specific ACF key and value from all posts – no access to DB
- I am officially missing something about transient posts
- Display upcoming events in the next 30 days by querying posts with timestamp in custom meta
- $wp_query->found_posts not returning correct value
- WP_Query, Paginate by Custom Field
- Post incorrectly excluded when using “category__in”?
- Display posts ONLY from the current logged in user and current week/year/month Elementor posts
- Replace wp_query with wp_user_query
- Block internal search queries with pre_get_posts and regex rules
- is_user_logged_in not working from WP-API (wp-json)
- Search Exact on WordPress – Relevannsi Solution Not Working
- Any way to use FETCH_KEY_PAIR with $wpdb?
- order posts by a secondary query that counts items
- Query against multiple locations within single custom post type post
- WP Query. Ordering posts by another post types meta
- Woocomerce – Order products by float attribute in archive pages
- How to make a search query if not found in post table then search in postmeta table?
- WP_Query about meta_key and its array value
- Create global array to compare inside a query
- how to show more than 1 post into three columns query
- How can I get all the posts that are related with a specific taxonomy term?
- Modify query to exclude certain ids and certain parents and its corresponding children
- Filter posts/pages by user_role array
- Pagination in custom query in a category page
- Get a list of the last posts grouped by author and filtered by category
- When should you use WP_Query vs query_posts() vs get_posts()?
- Sort posts in dashboard using custom field; also include posts where field isn’t set
- How to query post ids liked by the Author
- Modify query after meta value
- Get the number of rows from $wpdb->get_results not working
- Parsing External Table Arguments
- difference between methods of query
- Query posts by meta value and sort by another meta key
- how to use transient method?