Query order by Meta Value or Numeric Meta Value

Hi all,

when trying to sort a query by an ACF date I use
Order by Numeric Meta Value and set the key to my date ACF field as shown here:

While the query was working before adding the Order,
it doesn’t fire any error in the backend but disappears in the front end.

If I look at the Query code, it seems weird as the sorting key is set to NULL

<?php // The Arguments $args = array ( 'post_type' => array ( 0 => 'artshow', ), 'ignore_sticky_posts' => true, 'tax_query' => array ( 0 => array ( 'taxonomy' => 'artshow_type', 'terms' => array ( 0 => 41, ), 'include_children' => true, 'removeNoTerms' => false, 'title' => 'Type = group show', ), ), 'orderby' => 'meta_value_num', 'meta_key' => NULL, 'order' => 'ASC', ); // The Query $the_query = new WP_Query( $args ); What do you think? Thanks in advance, J

I have the same issue. I use a numeric value specifically created for sorting.
I tried ordering my posts with two variants
1:

<?php 
// The Arguments
$args = array (
  'post_type' => 
  array (
    0 => 'abgeordneter',
  ),
  'posts_per_page' => 20,
  'ignore_sticky_posts' => true,
  'orderby' => 'meta_value_num',
  'meta_key' => '',
  'order' => 'ASC',
);
// The Query
$the_query = new WP_Query( $args );

2:

<?php 
// The Arguments
$args = array (
  'post_type' => 
  array (
    0 => 'cpt_name',
  ),
  'posts_per_page' => 20,
  'ignore_sticky_posts' => true,
  'orderby' => 'meta_value',
  'meta_key' => '',
  'order' => 'ASC',
);
// The Query
$the_query = new WP_Query( $args );

No matter what I do the posts appear to be ordered bei post date.

Yeap!

I ended up querying through php.
the good thing though is that you can just use the Query code from the Query block,
you just need to inform the key using the ACF field you want the Query to be sorted by.

In the meantime @Louis that would be maybe a good idea if we could edit the Query code in order to fix these kind of issues until the Query block works fully…

How do you edit the code? I get the message that I can’t edit in read only mode.

you can’t edit the code, that is what I say in my post, telling Louis it could be good if we actually could to fix this kind of situations.
I was talking of creating a php snippet based on the Query code you got from the Query block (that same one you pasted in your previous message).

I understand. Thats too advanced for me. I’ll wait for the fix :stuck_out_tongue:

It depends what you need to display and how complex it is really,
I needed it to make a list of events displayed by dates like a resume style, so it wasn’t too hard to hand code as the layout was pretty simple really, just some text with links and some basic formatting.

Hmm – still I’ll leave it at that for now. At the moment I can work around by selecting the fitting publish date so the order is correct.

Just looking at this and I’m not quite sure I understand why you mark this as a bug.

The way you have set it up, you are asking the query to use the current ACF field value of the post/page the query is published on to order the query (that’s what dynamic fields are for).

If you want to order by the ACF field of every post available inside the query, you’ll want to use the simple static name of the ACF field… No need for dynamic data here.

2 Likes

You are right @Louis
finally go it working, so just moved it from Bugs to General, sorry!
I believe understanding the logic is not that easy at first…
Thank you for your patience and your help!

so @Jonas
it is actually easy
in the Query Block
when editing the Query
in Order
choose
Order by: Numeric Meta Value
and set as Key the name of your ACF field

and that’s it!!

1 Like

Unbelievable! I just finished changing all the dates of my posts so they are in the correct order :sweat_smile:
Very awesome! I already tried it and it works beautifully!