I have a custom-post-type named my_project. This post-type has two meta-boxes:

  • is_featured
  • is_featured_position

So essentially in my backend of the post-type I can set a checkbox for is_featured and enter a number for is_featured_position

enter image description here

This allows me to set a "project" to my homepage of my website.

I have a function called get_project_list() that should return all my "featured" projects.

function get_project_list( $order = 'DSC' ) {
    $args = array(
        'post_type' => 'my_project',
        'posts_per_page' => -1,
        'orderby' => 'meta_value_num',
        'order' => $order,
        'meta_key' => 'is_featured',
        // 'is_featured_position' ???

    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post();
        global $post;
        get_template_part( 'inc/project', 'item' );

This already works fine right now. The only question I have is how I can implement my "position" or "order" of my featured posts! All I want to do is additionally sort by the second meta_key is_featured_position which is a normal number.

So if I call get_project_list( 'DSC' ) I want all featured posts returend in the order that is set inside is_featured_position. Just like you know it from the normal wordpress Pages where you can set the Order of the list-pages function.

How can I filter by this second meta_key?

Thank you in advance.

1 Answer 1


You'll need to use meta_query along with meta_(key|value|compare) to interact with two different keys and get the order you want.

$q = new WP_Query(array(
    'post_type'      => 'my_project',
    'nopaging'       => true, // same as posts_per_page => 1,
    'order'          => $order,
    'orderby'        => 'is_featured_position', // your position key here
    'meta_key'       => 'is_featured_position', // also here, see below
    'meta_value_num' => 0, // you may want something different here
    'meta_compare'   => '>', // you may want something different here
    'meta_query'     => array(
            'key'     => 'is_featured',
            'copmare' => 'EXISTS', // only works in WP 3.5+

Okay, so when you use a combination of meta_(key|value|compare) keys along with meta_query some weirdness happens. WordPress uses the class WP_Meta_Query to parse and create metadata queries. We can take a look inside WP_Meta_Query::parse_query_vars to learn more.

 * Constructs a meta query based on 'meta_*' query vars
 * @since 3.2.0
 * @access public
 * @param array $qv The query variables
function parse_query_vars( $qv ) {
    $meta_query = array();

    // Simple query needs to be first for orderby=meta_value to work correctly
    foreach ( array( 'key', 'compare', 'type' ) as $key ) {
        if ( !empty( $qv[ "meta_$key" ] ) )
            $meta_query[0][ $key ] = $qv[ "meta_$key" ];

    // WP_Query sets 'meta_value' = '' by default
    if ( isset( $qv[ 'meta_value' ] ) && '' !== $qv[ 'meta_value' ] )
        $meta_query[0]['value'] = $qv[ 'meta_value' ];

    if ( !empty( $qv['meta_query'] ) && is_array( $qv['meta_query'] ) ) {
        $meta_query = array_merge( $meta_query, $qv['meta_query'] );

    $this->__construct( $meta_query );

The overall picture: meta_(key|value|compare) keys just become part of the meta_query array.

Unfortunately, WordPress only checks meta_key for a valid orderby, so you must include it as well as a meta_query with the remaining custom fields you want to use. In your case you must use the featured order field so you can use it the orderby argument.

The above is untested, but it should at least get you started!


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.