If we only have two stock statuses, namely outofstock
and instock
, we can very easily achieve sorting with pre_get_posts
add_action( 'pre_get_posts', function ( $q ) {
if ( !is_admin() // Target only front end
&& $q->is_main_query() // Only target the main query
&& $q->is_post_type_archive() // Change to suite your needs
) {
$q->set( 'meta_key', '_stock_status' );
$q->set( 'orderby', 'meta_value' );
$q->set( 'order', 'ASC' );
}
}, PHP_INT_MAX );
You should adjust that to your needs
Related Posts:
- Custom query filter not working on woocommerce category page
- Query Strings and Woocommerce
- Query Nickname rather than Display Name in custom Woocommerce plugin
- Category_in return empty
- How i make a custom sql query for Woocommerce
- WooCommerce Total # orders [closed]
- How to display SQL query that ran in query?
- WP_Query for WooCommerce Products
- Custom query_var causes displaying posts archive on front page
- What is the most efficient way of implementing a favorite post system?
- Why does get_the_time(‘F j’) return November 30 for all posts?
- Query by one meta_key and sort by another (possibly NULL value)
- Query & Sort Comments by custom comment meta
- Count user posts by user ID, Post type and Post status
- is_archive() doesn’t work on public query var archive pages?
- how to retrieve specific product attribute value in an sql query?
- WooCommerce – Query for ordered products
- search through post-type attachments titles
- Date query year and month OR just year
- cron job to auto delete posts of a specific post type older than x days
- get post id using the $query_vars variable
- Most effective use of DB querys
- mysql query paging
- Find woocommerce product ID by custom field value?
- Two differents queries in archive page
- How do I find if a page has a template?
- How to set back query var author_name after unset it?
- My SQL function to change user_name
- How select a specific query when setting offset?
- How to retrieve elements from another table in a SQL query?
- Change media attachment author via mysql query
- WordPress Loop: List All Posts by a Category & Subcategory
- Order query by post meta value
- Get all products which have both product category
- List all posts from past week grouped by sub-category
- Query Custom Post Type posts by Keyword/url slug
- how would I create a custom query to get all users, and a related post based on a postmeta field?
- Display posts with a start OR end date later than current date
- Order posts by custom field DATE value
- wpdb->term_taxonomy count posts from both published and private posts?
- Form redirect with query vars to page
- Query to Get All Users from wp_users With NO matching user_id In wp_usermeta
- WordPress custom setting page – add flashing message
- Custom query AFTER rss fetch_feed not working
- Attempt to improve WP search, can someone check my SQL query?
- Multiple Category Query
- list all categories that have a certain word in title
- Passing parameters to a static front page
- wp_list_pages() refuses to output posts
- Can’t seem to do combined query AND sort?
- sort query results by newest
- How to reset usual $query on search page to push custom $wpdb query there?
- get_posts showing only 5 posts. (nopaging and posts_per_page -1 not working)
- Order by meta value pro first then meta value free in my search function
- Display posts between two particular dates
- Order posts by date, but also give priority for a specific term
- How to delete all posts, categories and tags from WordPress database
- Ideas how to search & replace post_content when string contains a newline?
- Query the WordPress database to get data together with replaced information
- $wpdb->prepare affecting the query?
- WordPress SQL search, how to handle SQL Injection?
- Query for Custom Post Type UI Does Not Loop All the Post
- Updating with $qpdb->query() always returns 0 rows affected
- Wrap/close ‘query’ for a first-letter ordered listing of posts
- wpdb query with dynamic column name?
- How do you display the number of a post in a query?
- How to manage a particular “order by” for get_search_query()?
- Query to view scheduled or draft post
- How to add a Variable to post query
- get products by custom date variable
- Sort a list of a CPT ( job) if a meta is empty
- How to sort by two meta keys in admin area?
- How to filter result set in custom metaboxes?
- Restrict WordPress search to a single ACF field
- wp query multiple values > display a specific value first
- PHP Fatal error: Uncaught Error: Call to a member function insert() on null
- Nested select statements not working
- Restore WordPress Backup Locally?
- Trouble migrating custom post types from non-wordpress cms
- Pagination adds search query (again)
- Create global array to compare inside a query
- “order” does not affect order of custom query
- Create a new post on a specified publish date via link?
- Exclude Posts from a Widget
- Fastest and most efficient SQL query to check if UID exists
- New custom post type entries are not sorted correctly in admin using pre_get_posts
- Query String for the WP_QUERY parameters
- $wpdb query for price in custom field value
- Multiple terms not working on taxonomy
- Sorting of coupons in the right way
- search.php is not generating the right results
- excludeCat function reverses order of blog posts
- Run search query again without pagination gives no results?
- WP_Query orderby meta key/value suddely stopped working
- How to get one result using wpdb class?
- Delete query won’t run
- Where can I find the SQL to get the most used information by wordpress database?
- Modify a query using both a text field and a true/false AFC field
- Trying query woocommerce product order by menu order and stock status
- How to figure out correct wp_options to have autoload=’yes’