0

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();

1 Answer 1

2

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();
0

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.