Hot answers tagged

22 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
  • 339
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.2k
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
14 votes
Accepted

ORDER BY custom field value

You can create groups of meta_queries using specific compare operation on them, and since you want to order based in a single custom field, you can keep the order declaration dedicated to the single ...
Bruno Cantuaria'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,757
12 votes

Ignoring initial articles (like 'a', 'an' or 'the') when sorting queries?

An easier way may be to go through and change the permalink slug on those posts that need it (under the title on the post writing screen) and then just use that for ordering instead of the title. ie....
majick's user avatar
  • 5,071
10 votes

Order get_terms using a Custom Field

A much shorter solution, just add this before foreach: usort($terms, function($a, $b) { return get_field('issue_date', $a) - get_field('issue_date', $b); });
Grigur's user avatar
  • 201
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
9 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
  • 427
8 votes
Accepted

Ignoring initial articles (like 'a', 'an' or 'the') when sorting queries?

The Problem I think there's a typo in there: The name of the filter is posts_fields not post_fields. That could explain why the title2 field is unknown, because it's definition isn't added to the ...
birgire's user avatar
  • 66.9k
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
6 votes

Ignoring initial articles (like 'a', 'an' or 'the') when sorting queries?

EDIT I have improved the code a bit. All code block are updated accordingly. Just a note though before jumping into the updates in the ORIGINAL ANSWER, I have set up the code to work with the ...
Pieter Goosen's user avatar
6 votes
Accepted

get_the_terms in descending alphabetical order

You could try this: // Get posts terms $terms = wp_get_post_terms( $post->ID, 'winetype', array( 'order' => 'DESC') ); $sep = ', '; foreach ( $terms as $term ) { echo $term->name; ...
Robbert's user avatar
  • 1,245
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
  • 676
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

Sorting posts by custom fields in meta_query

As of WordPress 4.2, you can combine meta_query and orderby. The following example is taken from make.wordpress.org. Example of a simple meta_query: $q = new WP_Query( array( 'meta_query' => ...
fischi's user avatar
  • 7,489
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.2k
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

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,066
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's user avatar
3 votes

Advanced WP_Query with meta_query, orderby?

You should really read the documentation on meta_query's Here is what is allowed in a meta_query key (string) - Custom field key. value (string|array) - Custom field value. It can be an array only ...
Pieter Goosen's user avatar
3 votes
Accepted

WooCommerce: Add Payment Gateway Field to Webhooks

Thanks, will do next time. I figured this one out: function my_custom_wc_api_order_response( $order_data, $order, $fields ) { $customer_info = get_post_meta( $order->id, '_customer_info', true );...
lawdawg's user avatar
  • 51

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