3

I'm trying to return all the images within the media library that have a custom field with the value of True.

For some reason nothing is being returned, this is what I have so far:

function showPainted() {
    $query_images_args = array(
        'post_type'      => 'attachment',
        'post_mime_type' => 'image',
        'post_status'    => 'inherit',
        'posts_per_page' => - 1,
        'meta_key'      => 'show_on_painted_page',
        'meta_value'    => '1'
    );

    $query_images = new WP_Query( $query_images_args );
    $images = array();
    foreach ( $query_images->posts as $image ) {
        $images[] = wp_get_attachment_url( $image->ID );
    }

    print_r($images);
}
add_shortcode( 'showPainted', 'showPainted' );

Can anyone help me with this one?

WebDevB
  • 492
  • 2
  • 7
  • 25

2 Answers2

2

Please change your query agrs as:

$query_images_args = array(
            'post_type'      => 'attachment',
            //'post_mime_type' => 'image',
            'post_status'    => 'any',
            'posts_per_page' => -1,
            'meta_key'      => 'show_on_painted_page',
            'meta_value'    => true
        );

        $query_images = new WP_Query( $query_images_args );
Gufran Hasan
  • 8,910
  • 7
  • 38
  • 51
0

Try something like this:

$query_images_args = array(
    'post_type'         => 'attachment',
    'post_mime_type'    => 'image',
    'post_status'       => 'inherit',
    'posts_per_page'    => - 1,
    'meta_key'          => 'show_on_painted_page',
    'meta_query'        => array(
        array(
            'key'       => 'show_on_painted_page',
            'value'     => 1,
            'compare'   => '='
        ),
    ),
);

Check https://codex.wordpress.org/Class_Reference/WP_Query for more informations.

Alex
  • 220
  • 2
  • 8