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'm looking for a way to only show posts results that contain all of the custom fields requested. Not one or the other. I was only able to get results with one array at a time. Any help would be appreciated.

        $documents = array(
        'post_type' => 'documents',
        'meta_query' => array(
            array(
                'key' => 'document-type',
                'value' => 'manual',
            ),
            array(
                'key' => 'document-status',
                'value' => 'current',
            )
        )
     );
        query_posts( $documents );
        get_template_part( 'loop', 'documents' );
        wp_reset_query();

I also tried an SQL Query without any luck.

         $documents = "
            SELECT wposts.*
            FROM $wpdb->posts wposts
            JOIN $wpdb->postmeta document-status ON
              ( wposts.ID = document-status.post_id AND
                document-status.meta_key = 'document-status' )
            JOIN $wpdb->postmeta document-type ON 
              ( wposts.ID = document-type.post_id AND
                document-type.meta_key = 'document-type' )
            WHERE document-status.meta_value = 'current'
            AND document-type.meta_value = 'manual'
            AND wposts.post_status = 'publish'
            AND wposts.post_type = 'documents'
            AND wposts.post_date < NOW()
            ORDER BY wposts.post_date DESC
            LIMIT 0 , 4
         "; 
         $documents = $wpdb->get_results($documents, OBJECT);
        query_posts( $documents );
        get_template_part( 'loop', 'documents' );
        wp_reset_query();
share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

meta_query has a relation parameter just like tax_query so:

$documents = array(
    'post_type' => 'documents',
    'meta_query' => array(
    'relation' => 'AND',
        array(
            'key' => 'document-type',
            'value' => 'manual',
        ),
       array(
            'key' => 'document-status',
            'value' => 'current',
        )
    )
);
query_posts( $documents );
get_template_part( 'loop', 'documents' );
wp_reset_query();
share|improve this answer
add comment

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.