WP_Query does not work after adding meta_query

1

I have the following query:

// WP_Query arguments
$args = array (
    'posts_per_page'         => '6',
    'post_type'              => 'attachment',
    'meta_query'             => array(
        array(
            'key'       => 'galeriaimg',
            'value'     => 'sim',
        ),
    ),
);

// The Query
$attachment = new WP_Query( $args );

// The Loop
if ( $attachment->have_posts() ) {
    while ( $attachment->have_posts() ) {
        $img_url = wp_get_attachment_url( $attachment->ID, 'medium', false );
        $img_url_full = wp_get_attachment_url( $attachment->ID, 'large', false );
        echo '<a class="fancybox" href="'.$img_url_full.'">';
        echo '<img src="'.$img_url.'">';
        echo '</a>';
    }
} else {
    echo '<h1>ERRO1</h1>';
}

// Restore original Post Data
wp_reset_postdata();
?>

For some reason, after I added the custom field (meta key) in the query, it stopped returning the images. I already checked the database and the images are related to the meta key, all right, including the value.

What's the problem?

    
asked by anonymous 13.03.2014 / 03:18

1 answer

1

Matheus, apparently the arguments of your query are correct.

If it's still not working, try using a comparison operator on one of the nodes in the meta_query array (and run tests with different operators to see if changes in the behavior of the results occur):

'meta_query'             => array(
    array(
        'key'       => 'galeriaimg',
        'value'     => 'sim',
        'compare    => 'IN',
    ),
),

By default the operator is the "=", which should already result in your expected result .. but test (other available operators in the documentation )

    
08.04.2014 / 18:15