Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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' );
    endwhile;
}

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.

share|improve this question

1 Answer 1

up vote 0 down vote accepted

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.

<?php
$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(
        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!

share|improve this answer
    
Perfect, thank you! –  mathiregister Jan 3 '13 at 14:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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