Query posts by Post title

admin2025-06-03  2

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 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 );
Share Improve this question edited Feb 10, 2019 at 16:08 Alexander Holsgrove 1,9091 gold badge15 silver badges25 bronze badges asked Feb 8, 2019 at 10:50 Peter DieperinkPeter Dieperink 11 2
  • I don't think this code works. You cannot use curly quotes () in PHP. :) – fuxia Commented Feb 8, 2019 at 10:52
  • @fuxia - you beat me to the edit :) – Alexander Holsgrove Commented Feb 8, 2019 at 10:53
Add a comment  | 

2 Answers 2

Reset to default 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.

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/reference/classes/wp_query/#order-orderby-parameters

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748922220a314843.html

最新回复(0)