Wp_query does not work after adding meta_query

Asked

Viewed 141 times

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 to me. I already checked the bank and the images are related to the meta key, everything straight, including the value.

What’s the matter?

1 answer

1

Matheus, apparently your query arguments are correct.

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

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.