0

I use the following ACF query to generate a list, works perfectly.

Now, however, I want to order my list by standard WP “post title” instead of ACF custom field “datum”

How can I do that ?

// args
$args = array(
   'posts_per_page' => 999,
   'post_type'   => 'lezing',
   'orderby' => 'meta_value',
   'order' => 'ASC',
   'meta_key' => 'datum',
   'meta_query' => array(
      array(
         'key' => 'jaar',
         'value' => '2019',
      ),
   )
);

// query
$the_query = new WP_Query( $args );
  • I don't think this code works. You cannot use curly quotes () in PHP. :) – fuxia Feb 8 '19 at 10:52
  • @fuxia - you beat me to the edit :) – Alexander Holsgrove Feb 8 '19 at 10:53
1

That query has nothing to do with ACF. It's a regular post query using WordPress' standard query class. As such you can refer to the documentation for the options for ordering your query.

To sort by post title you just need to set orderby to title:

<?php
$args = array(
    'posts_per_page' => 999,
    'post_type'      => 'lezing',
    'orderby'        => 'title',
    'order'          => 'ASC',
    'meta_query'     => array(
        array(
            'key'   => 'jaar',
            'value' => '2019',
        ),
    ),
);

$the_query = new WP_Query( $args );

Also note that the quote marks in your original question are incorrect. You need to use non-fancy quotes like '. This can be an issue if you're copying code from incorrectly formatted blog/forum posts.

| improve this answer | |
0

Your query will order using the meta term. To order by the post title, you just need to change the orderby to title.

Take a look at the documentation for what the parameters mean: https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters

| improve this answer | |
  • Thank you very much, now I understand. I have changed the code and it works! Thanks again, Greetings Peter – Peter Dieperink Feb 9 '19 at 12:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.