0

I have written the following query to fetch a list and want to show the list of names in ascending order.

I was earlier trying to pass meta_key and meta_value as arguments but it would not give the sorting correctly.

After trying meta_query I'm still stuck with the same problem.

$connected = new WP_Query(
    array(
        'connected_type'    => 'posts_to_pages',
        'connected_items'   => get_queried_object(),
        'nopaging'          => true,
        'post_type'         => 'attorney',
        'attorney_category' => 'partner',
        'post_status'       => 'publish',
        'posts_per_page'    => - 1,
        'caller_get_posts'  => 1,
        'orderby'           => 'order_clause',
        'order'             => 'ASC',
        'meta_query'        => array(
            'order_clause' => array(
                'key' => 'Last name',
            )
        )
    )
);

The following is the error I get when I print the query result:

Screenshot of the error

  • Are you sure the key name you used is correct: "Last name"? Or is it something like wpcf-last-name? Please check in database - wp_postmeta table. – Jiten Gaikwad Jun 19 '17 at 18:59
  • yeah i checked in database and key name is proper. – Shravan Jun 19 '17 at 19:09
0

I don't believe order_clause is an acceptable orderby parameter. According to the documentation you will need to add a meta_key to your query if you want to orderby a meta_value.

I have amended your query to display attorney posts where the custom field key Last name is present regardless of the custom field value. orderby is set to meta_value which refers to the meta_key which has been set to Last name.

$connected = new WP_Query(
  array(
    'connected_type' => 'posts_to_pages',
    'connected_items' => get_queried_object(),
    'nopaging' => true,
    'post_type' => 'attorney',
    'attorney_category'=> 'partner',
    'post_status' => 'publish',
    'posts_per_page' => -1,
    'caller_get_posts'=> 1,
    'meta_key'  => 'Last name',
    'orderby' => 'meta_value title',
    'order' => 'ASC',
  )
);

Presuming your custom field is actually called Last name this query should work.

You could add a second orderby parameter such as title as a fallback:

'orderby' => 'meta_value title'

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.