I want to loop through posts and show only those posts for which the custom field
i.e _shopis not empty

I am always confused with meta query and even not sure that if this is possible using this or not . Following is what i have tried so far

$args = array('post_type' => 'jobs',
              'posts_per_page' => 12,
              'paged'=> $paged,
              'tax_query' => array(
                            array(
                                'taxonomy' => 'job_category',
                                'field'    => 'slug',
                                'terms'    => $term->slug,
                            ),
                        ),
               'meta_query' => array(
                              array(
                                'key' => '_shop',
                                'value' => '',
                                'compare' => '!='
                                )
                              )
                'meta_key' => '_shop', 
        );
$loop = new WP_Query( $args ); 

1 Answer
1

To select posts that have a meta value, use the EXISTS comparison operator.

$args = array(
    'post_type' => 'jobs',
    'posts_per_page' => 12,
    'paged'=> $paged,
    'tax_query' => array(
        array(
            'taxonomy' => 'job_category',
            'field'    => 'slug',
            'terms'    => $term->slug,
        )
    ),
    'meta_query' => array(
        array(
            'key'     => '_shop',
            'compare' => 'EXISTS'
        )
    )
);

Leave a Reply

Your email address will not be published. Required fields are marked *