Skip to main content
23 votes

Sort on meta value but include posts that don't have one

Easy Peasy, just tested 2018, using in production currently. Update 2022: Modified so that the NOT_EXISTS query comes before the EXISTS query, and clarifies the effect that multiple keys has on the ...
noahmason's user avatar
  • 357
19 votes
Accepted

WP_Query order by multiple meta keys & fields

You are using meta query without setting a value. The way you are doing it is using to query posts, not to order them. Using Named Meta Queries To order your posts by different meta datas, you can ...
Johansson's user avatar
  • 15.3k
16 votes
Accepted

WP_Query orderby date not working

This will definitely work....It worked for me... $username = get_the_author_meta( 'login', $author_id ); $args = array( 'post_type' => 'any', 'orderby' => 'date', 'order' => '...
Pravin Work's user avatar
13 votes
Accepted

Query posts ordering by title, but ignore " and special characters

Try this... $posts = get_posts( array( "orderby"=> "slug", "order" => "ASC", "post_type" => "my-custom-post-type", "posts_per_page" => -1, "...
RiddleMeThis's user avatar
  • 3,767
11 votes

WP_Query - Order results by meta value

if your meta value is not numeric for example if it is a date value; than you can add parameter meta_type => DATE. Possible values are; 'NUMERIC', 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', '...
ewroman's user avatar
  • 457
10 votes
Accepted

Sort posts by Date (DESC) and by Title (ASC)

You can pass an array to the query as the following example described in the Codex shows: $args = array( 'orderby' => array( 'title' => 'DESC', 'menu_order' => 'ASC' ) ); $query = new ...
Cesar Henrique Damascena's user avatar
9 votes

Sort on meta value but include posts that don't have one

The problem that everyone here is having has to do with the order of the meta queries. In order to sort correctly, you will need to put the "NOT EXISTS" query before the "EXISTS" query. The reason ...
Paul's user avatar
  • 958
7 votes

Adding 'menu order' column to custom post type admin screen

@anu let me to the right direction, however that is not a modern code Modern solution && working on WordPress 5.4 add support register column display column value make the registered column ...
jave.web's user avatar
  • 477
7 votes

Is it possible to paginate posts correctly that are random ordered?

Since the last versions of WordPress, you can now add a seed to the value of the orderby parameter of WP_Query: $query = new WP_Query([ 'orderby' => 'RAND(' . $seed . ')', ... ]); $seed ...
Guicara's user avatar
  • 210
5 votes
Accepted

How do I get the latest note on the order at woocommerce?

The order notes are saved as post comments, so you can use the WordPress function get_comments() to get the last note: $args = array( 'post_id' => $order_id, 'orderby' => 'comment_ID', ...
ahendwh2's user avatar
  • 696
5 votes
Accepted

How can I pick a single post from the latest 3?

Here's my approach... First you have to select 3 latest posts, then you have to pick random one of them... But it's easier to shuffle selected posts than picking only one of them - that way you can ...
Krzysiek Dróżdż's user avatar
5 votes
Accepted

How to get woocommerce selected variation from order object

Following below code according your need To get the selected variation attributes using get_variation_attributes( ) method. // Get an instance of the WC_Order object from an Order ID $order = ...
HK89's user avatar
  • 706
4 votes

Order Admin sub-menu items?

I know I'm necro-posting but I recently had the same issue. Please note that I used Rao's solution on SO to get to this answer. Assuming your CPT is called "notes". /** * @see https://codex....
StephanieQ's user avatar
4 votes
Accepted

Why wordpress popular post widget shows Undefined index?

This notice appears when you try to access an undefined index of an array. To fix this, you'll need to check if the index exists before you access it. For this you can use isset() or array_key_exists(...
Ethan O'Sullivan's user avatar
4 votes

Wp query orderby 'title' doesn't work

I came across the same problem and could not figure it out. Eventually found the problem when I took a look at the custom post type entries in the database and noticed that the results that were out ...
philip's user avatar
  • 141
4 votes
Accepted

Orderby Post Views

You can sort your query by view number, using the meta key : $query = new WP_Query( array( 'meta_key' => 'post_views_count', 'orderby' => '...
Johansson's user avatar
  • 15.3k
4 votes
Accepted

how to get post order by post id wp_query?

OK, so you want to define posts order by yourself. WP_Query allows you to do that - you'll have to use orderby => post__in to achieve it. And that's what you do. So why isn't it working? Because of a ...
Krzysiek Dróżdż's user avatar
3 votes
Accepted

Query random post from different categories

Random ordering is quite expensive operations in SQL and can become a headache on very big sites. Getting a random post from a category for 6 categories will mean you might need to run 6 separate ...
Pieter Goosen's user avatar
3 votes

Order by DESC, ASC in custom WP_Query

ORDER BY ID ASC $args = array( 'order' => 'ASC', 'orderby' => 'ID', ); or ORDER BY ID DESC $args = array( 'order' => 'DESC', 'orderby' => 'ID', );
Donn Frederick Lucas's user avatar
3 votes

How to quickly reorder posts in the admin panel that will persist for the wp-api

Let's assume it stores the custom order into the menu_order column in the wp_posts table. If you mean the hierarchical page post type (supports page-attributes) then one can order with the query ...
birgire's user avatar
  • 67.5k
3 votes

Using Orderby and meta_value_num to order numbers first then strings

I found this solution by combining code by @bonger and https://stackoverflow.com/questions/18084199/wordpress-query-order-by-case-when And it works well. Function function filter_case($orderby = '')...
Yatix's user avatar
  • 432
3 votes

Arrange Posts By Date In Order Of Closest To The Current Date

I know this is old, but ordering by DESC does not make sense for this case. You select DATEs >= CURRENT_DATE, now you want to display first the one closest to today, let's say tomorrow and then ...
jave.web's user avatar
  • 477
3 votes

Query Posts in a Predefined Order

Based on @Stephen Harris answer, for Wordpress 3.5+ the easiest option seems to be ordering by post__in. Here is the example: $post_ids = Array(12, 15, 18, 8, 10, 5); $args = array( 'post_type' =&...
jmarceli's user avatar
  • 1,076
3 votes

Sort on meta value but include posts that don't have one

I also encountered a similar problem and the following solution helped me: $args = array( 'post_type' => 'kosh_products', 'posts_per_page' => -1, 'meta_query' => array( 'relation' => '...
Ilya Kogan's user avatar
3 votes
Accepted

Author list 'special' author first then 'normal' author

The 'special' authors have in usermeta a meta key called 'werksortgodmodus' with meta value 'yes'. The 'rest' of the authors have 'werksortgodmodus' = no . You could e.g. order by the meta value ...
birgire's user avatar
  • 67.5k
3 votes
Accepted

How to use order RAND() on Wordpress?

You are right that you can't have WordPress order the retrieved posts randomly. So you'll have to do that yourself after you have retrieved the array of post objects. Let's call that $my_posts. ...
cjbj's user avatar
  • 14.8k
3 votes

WP_Query orderby date not working

Adding 'suppress_filters' => true into the $args array did sorting in the order I needed.
Vital Rulinski's user avatar
3 votes
Accepted

Wordpress Ordering Problem. How to fix ordering 1-10-100 issue?

Based on the research I've done on this, using term meta data is a better all around approach to ordering terms. However, I was able come up with this snippet which does sort the terms by their name, ...
Dave Romsey's user avatar
  • 17.8k
3 votes

Remove action from woocommerce barcode plugin

You need to access the class variable to remove a action which has been added within a class function remove_default_order_barcode(){ global $WooCommerce_Order_Barcodes ; remove_action( '...
Harry's user avatar
  • 151
3 votes

How can I set custom order use this function

You will need to add term meta to your categories. In this metafield you will store the string or number that you want to use as a base for your custom order (tutorial). Let's say you have generated a ...
cjbj's user avatar
  • 14.8k

Only top scored, non community-wiki answers of a minimum length are eligible