0

I have two custom post types

  1. case
  2. client

In case post type I have a ACF custom field to choose a client, so each case have a single client.

Now what is working: I am displaying all cases on a page and able to filter them using a client Id.

Whats I want: I want to get a list of cases (of all clients) and the order by sorting should be a client title ASC.


I am able to apply order by using client Id but I want order by client title.

Please suggest an solution for this.

$args = array(
        'post_type' => 'case',
        'post_status' => 'publish',
        'orderby'   => 'meta_value_num',
        'meta_key'  => 'client',
        'posts_per_page' => 9
     );
1
  • Can you share your ACF custom field code here as well as post type creation code. Jun 5, 2020 at 12:54

2 Answers 2

2

You can use the order and orderby param in WP_Query argument like below:

$args = array(
        'post_type' => 'case',
        'post_status' => 'publish',
        'meta_key'  => 'client',
        'posts_per_page' => 9,
        'orderby' => 'title',
        'order'   => 'ASC',
);
$query = new WP_Query( $args );

It will list out your posts by title. For more information check official WordPress document

5
  • There are two issue with above: 1. comma(,) is mising after ``'posts_per_page' => 9``` 2. This query returning 0 result Jun 5, 2020 at 7:49
  • Thanks for suggesting, I have updated code. May be you have added orderby twice in parameter. Also confirm once your post type slug to use in query. Jun 5, 2020 at 7:57
  • It is not working Jun 5, 2020 at 12:11
  • Here is the query as per your args SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) WHERE 1=1 AND ( wp_postmeta.meta_key = 'client' ) AND wp_posts.post_type = 'case' AND ((wp_posts.post_status = 'publish')) GROUP BY wp_posts.ID ORDER BY wp_posts.post_title ASC LIMIT 0, 9 Jun 5, 2020 at 12:11
  • Check if you are writing proper query with meta_key. Also make sure you have a posts in that post type. I have added only two filter in that query to sort by title. Jun 5, 2020 at 15:38
1

You can try this:

$args = array(
     'post_type' => 'case',
     'post_status' => 'publish',
     'orderby'   => 'meta_value',
     'order' => ASC,
     'meta_key'  => 'client',
     'posts_per_page' => 9
);

More information of orderby parameters

2
  • Welcome to WordPress Development and thanks for contributing to our pool of answers. Our site is different from most - if you have not done so yet, consider checking out the tour and help center to find out how things work. Jun 6, 2020 at 15:06
  • Thanks, @MatthewBrownakaLordMatt I'll check it out.
    – Rod
    Jun 7, 2020 at 11:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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