Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

WordPress

Using multiple custom fields for 'orderby' in 'WP_Query'

Hi, everyone!

I've been taking Treehouse Wordpress lessons for a while and now working on a bilingual dictionary (spanish-turkish) plugin and a template for it.

In 'search.php', i want my WP_Query entry/post) list ordered by two of my custom fields and one of wp default parameter:

, orderby (word_count title word_type),

" -First, order my entries by word_count. -If you have many word_count=1 results, order these by their titles. -Than finally if there is more than one entry, both the word_count and title of them are equal, then order these nearly impossible entries by their word_type. "

After serching: 'xyz'

What i want to see in search page is like: xyz / adj xyz / noun xyz / verb abc xyz / verb xyz abc / noun xyz abc ghk / noun

My questions to you all, vise and handsome or pretty and six-packed and cute and intelligent people, are:

  1. Is it possible to use 3 parameters in the orderby field?
  2. Is it possible to use more than one custom field parameter in the orderby field?
  3. If i'm on the right track could you show or link me the correct sample code?

Many thanks indeed.

Onur.

4 Answers

Andrew McCormick
Andrew McCormick
17,729 Points

You probably can do so with meta_query Custom Field Parameteres, Wordpress Codex

Maybe something like this:

<?php
$args = array(
    'post_type'  => 'words',
    'meta_query' => array(
        array(
            'key'     => 'word_count',
                        'orderby' => 'meta_value_num',
                        'order' => DESC
            ,
        ),
        array(
            'key'     => 'title',
                        'orderby' => 'meta_value',
                        'order' => DESC
        ),
        array(
            'key'     => 'word_type',
                        'orderby' => 'meta_value',
                        'order' => DESC
        ),
    ),
);
$query = new WP_Query( $args );
?>
Zac Gordon
STAFF
Zac Gordon
Treehouse Guest Teacher

May also be interested in what core WP dev mentions around 17 min in or so in a recent WordCamp Europe talk where he mentions so querying abilities coming in 4.1, the next WP release. http://wordpress.tv/2014/10/13/andrew-nacin-post-modern-wordpress/

Andrew McCormick
Andrew McCormick
17,729 Points

Zac, Thanks for that link! Really looking forward to having better[easier] queries.

Thank you Zac, it seems we won't be waiting for so long for new version.

Thank you Andrew,

The point is ... :

<?php

global $query_string;
$query_string = get_search_query();
$query_post_type = get_query_var('post_type');

// below is your code:
$args = array(
    's'             => $query_string,
    'post_type'     => 'words',
    'meta_query'    => array(
                            array(
                                'key'     => 'word_count',
                                'orderby' => 'meta_value_num',
                                'order' => DESC,
                            ),
// i don't have a custom field named 'title'.
// i used the default 'title' field of the regular post type
// when defining my custom post type:
// ... 'supports' => array( 'title', 'author', 'comments' ), ...
                            array(
                                'key'     => 'title',
                                'orderby' => 'meta_value',
                                'order' => DESC,
                            ),
                            array(
                                'key'     => 'word_type',
                                'orderby' => 'meta_value',
                                'order' => DESC,
                            ),
                        ),
        );

$query = new WP_Query( $args );

// below is my old code:
$search = new WP_Query(array('s' => $query_string, 'post_type' => $query_post_type,
'orderby' => 'word_count title word_type', 'order' => 'ASC')) ;
?>

The query is completely and perfectly done with the 's' keyword. I need nothing more to refine it. I just need to use multiple custom field values for ordering my query results.

If i needed to order a standard post type query, this code should have done the work:

<?php
$search = new WP_Query(array('s' => $query_string, 'post_type' => $query_post_type,
'orderby' => 'author(first_par) title(second_par) comment_count(third_par)',
'order' => 'ASC')) ;
?>

This is the final code for using multiple 'orderby' parameter in WP_Query. One is the predefined post parameter 'title', other one is a meta key of a custom post type.

It seems, for Wordpress 4.0:

  • Using more than one predefined parameter is possible.
  • Using more than one custom field meta key is not yet possible. Anyone, who proves this is wrong, will be highly appreciated in my case
<?php
global $query_string;
$query_string = get_search_query();

$search = new WP_Query(array('s' => $query_string, 'post_type' => 'word',
'orderby' => 'meta_value title', 'meta_key' => 'word_type', 'order' => 'ASC')) ;
?>