$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Query posts by Post title|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Query posts by Post title

matteradmin9PV0评论

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

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far