Use pre_get_posts
to alter the main query before it is run. The action runs on every query, use the Conditional Tags to target specific queries.
function wpd_sort_by_meta( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'meta_key', 'your_meta_key' );
$query->set( 'orderby', 'meta_value_num' );
}
}
add_action( 'pre_get_posts', 'wpd_sort_by_meta' );
Related Posts:
- How to get order of posts?
- Loop through all tags & output posts in alphabetical list
- Woocommerce custom loop to show all the products [closed]
- How do I sort this custom list of sticky posts
- Display three sequential posts on each page load, without repeating previous
- Sorting is not working in WordPress WP_Query
- Can I force WP_Query to return no results?
- Ignoring initial articles (like ‘a’, ‘an’ or ‘the’) when sorting queries?
- Multiple WP_Query loops with Pagination
- Sorting: custom query with orderby meta_value_num THEN by title
- WP_Query and next_posts_link
- Too slow when using both ‘tax_query’ and ‘meta_query’ both in WP_Query
- Get array of posts from the current archive page loop
- How can I save an array from a random post sequence for later use?
- In loop: posts have thumbnail AND other variables
- How to order category.php loop by ‘meta_value’?
- Add inline HTML to posts published within last 24hrs
- Move posts to top of WP_Query if in certain Taxonomy?
- Ajax Infinite Scroll In Custom WP_Query Loop Not Working
- Sorting meta_value as integer doesn’t work
- Display different number of posts from one category on the different pages
- Pagination not working Search posts
- Sort in WP_Query(), not filter? Is it possible?
- wordpress query in header won’t reset and corrupts other loops
- Why does the_content not work like others for a set post id?
- 2 loops on page – one with orderby rand second orderby date
- Undefined WP_Query::has_posts()?
- Create ONE callback for all page templates, post filter queries + paginated pages, triggering pagination via AJAX
- How to use WP_Query to display many posts?
- Counter problem, infinite loop when post_per_page equals X
- How to vary post loop results layout and resume?
- Five posts from a category in footer
- How to get all unique categories for posts in loop?
- WordPress Loop and $post
- Using the_post_thumbnail resets current loop item ID
- Order by empty custom field
- Excluding pages in WP_query using ACF
- reset to main loop doesnt work
- Help ordering Post loop by two meta values
- Some doubts about how the main query and the custom query works in this custom theme?
- New WP_Query loop in admin causes problems
- How to sort a custom wordpress query by combination of meta values?
- Only display post if published in last 24 hours?
- Need to convert string of term ids into comma separated integers for use in an arguments array
- How to remove the most recent post from $the_query
- WP_Query loop else statement not executing
- How do I get my custom query to work with search results after the first page?
- Issue with front page navigation after upgrading to 3.4
- Check for the main query from the template
- I am having problem sorting custom post type using WP_Query
- Exclude recently updated post from custom WP_Query using multiple loops
- get_posts output always same post
- How to use WP_Query() on single.php?
- difference between querying database and using the loop
- Stomping WP_Query in author archive to facilitate pagination with custom queries
- Loop increase in while loop not working
- posts archive page – closing WP_Query loop correctly [closed]
- AJAX Breaking Offset Argument In WP Query
- Continue or break the while loop
- Loop posts based on permalink term
- Check if a post has term inside loop
- Display all posts in a custom post type, grouped by a custom taxonomy. How to sort the posts alphabetically and the terms by ID?
- WP Query and date format
- How to Order By Two Custom Fields?
- Display specific page (that is child) content on parent page
- Does putting queries within loops cause an issue on WordPress?
- Function using get_posts() with tax_query not working when called from functions.php
- Paginate pages with dynamic query
- WP_Query loop doen’t work with my custom taxonomy
- How to make the ‘request’ filter work?
- Order (by ASC) posts with meta_key so posts without values are last
- how to include orderby value that is empty?
- How to sort wordpress posts already selected by WP_QUERY
- How sort products by calculate value? ( custom post meta, price, option)
- show most viewed post
- How do I stop the same post showing multiple times in a archive?
- Order by value of Custom Field using url string
- how to get custom attachment url?
- Case insensitive ORDERBY in wpquery
- WP_Query & Duplicate entries
- How to mix two orderby-parameters into one ordered result
- Calling a function with WP_Query only ever brings the first result
- Combine WP_Query with array of custom data to single loop without breaking the pagination
- query_posts() doesn’t seem to be called in my page
- Pagination returns 404 after page 20
- WP_Query a custom field value bringing back 0 results
- Set Transient on CPT
- Create multiple sections for all categories and then queries all the posts for each of those categories
- query_posts with sorting on a custom datestamp
- Next / previous posts link doesn’t show up with Posts 2 Posts
- meta_query dates from an array
- Creating array to compare custom field values
- wordpress sorting using array merge by price in ascending order but price with 0 must be show last
- Carousel Loop only duplicating
- How to show featured post first, then separate loop for other posts
- Show linked products using wp query and woocommerce
- Standard Loop – wp_query
- Custom Wp_query loop takes very long
- Pagination only showed when no category is set in wp_query
- Sort by multiple columns using get_posts