I want to query posts where meta value is empty. for example, I want to get these three posts, with no meta values:
enter image description here

Already tried:

$args = array(
    'post_type'   => 'attachment',
    'posts_per_page' => 10,
    'paged'          => $paged,
    'meta_query'  => array(
        array(
            'key' => '_wp_attachment_image_alt',
            'value' => '',
            'compare' => 'LIKE'
        )
    )
);
$attachments = new WP_Query($args);

and:

$args = array(
    'post_type'   => 'attachment',
    'posts_per_page' => 10,
    'paged'          => $paged,
    'meta_query'  => array(
        array(
            'key' => '_wp_attachment_image_alt',
            'value' => null,
            'compare' => 'LIKE'
        )
    )
);

But it doesn’t work..

Any idea how to solve this?

Thank you

2 Answers
2

I think you forgot about the inherit post status. The default one in WP_Query is publish.

You should also use = instead of LIKE, to avoid using LIKE '%%' in the SQL query.

So try to add this:

'post_status' => 'inherit'

and

'compare' => '='

into your query arguments, to match the empty _wp_attachment_image_alt string values.

Leave a Reply

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