3

(Wordpress 4.3.1)

I'm running a custom loop and only want to include posts that have file uploaded to a custom field: audio_file.

I have tried many solutions around the web and so far nothing works.

Can I use meta_query to check if a key has a value?

Here is my current attempt:

 global $post;      

 $args = array(
    'post_type'      => array (
                        'podcast',
                        'event',
                        ),
    'posts_per_page' => 10,
    'post_status'    => 'publish',     
    'paged'          => get_query_var( 'paged' ),
    'meta_query'     => array (
                            'key' => 'audio_file',
                            'value' => '' ,
                            'compare' => '!=',
                            //'type'    => 'date',
                             ),
    );

    global $wp_query;

    $wp_query = new WP_Query( $args );

    if ( have_posts() ) { while ( have_posts() ) { the_post(); 

The intended result here is to display posts where 'audio_file' is not empty.

Thanks. (Wordpress 4.3.1)

1
  • Also, updating WP to latest version is always a good idea from the security point of view (especially if other people have tested it and proved that it's safe and doesn't break everybody's sites or plugins).
    – N00b
    Dec 16, 2015 at 23:47

4 Answers 4

5

I figured it out, meta_query must be an array within an array as it is intended for advanced queries using 'relation'.

meta_query => array (
    array (
        //'relation' => 'OR',
        'key' => 'audio_file', //The field to check.
        'value' => '', //The value of the field.
        'compare' => '!=', //Conditional statement used on the value.
    ),  
),

The conditional '!=' not-equal to '' (null) returns true if a file has been uploaded.

3

This might help someone. This will look for meta value, if exists and is not empty.

'meta_query' => [
    'relation' => 'AND',
    [
        'key'     => 'your_key',
        'compare' => 'EXISTS',
    ],
    // Don't want empty values
    [
        'key'     => 'your_key',
        'value'   => '',
        'compare' => '!=',
    ],
],
2

Can I use meta_query to check if a key has a value?

Any value? Use EXISTS

'meta_query'     => array (
                        'key' => 'audio_file',
                        'value' => '' ,
                        'compare' => 'EXISTS',
                        'type'    => 'date',
                         ),
);

I don't know why your uploaded file would be a date type though. I'd expect it to binary or maybe char

2
  • Right... missed that from a previous attempt to detect the date field. Perhaps 'type' is unnecessary anyway... I tried EXISTS and no luck...
    – AJD
    Dec 16, 2015 at 21:58
  • Yes, it may not be necessary at all
    – s_ha_dum
    Dec 16, 2015 at 22:02
2

It seems the type of value is essential now. I couldn't get the meta query working correctly without using the type. Significantly, while checking the value existence.

This code doesn't work now.

'meta_query' => array(
    'relation' => 'AND',
    array(
        'key'     => 'meta_key',
        'value'   => '',
        'compare' => '!=',
    ),
),

This one works.

'meta_query' => array(
    'relation' => 'AND',
    array(
        'key'     => 'meta_key',
        'value'   => '',
        'compare' => '!=',
        'type' => 'NUMERIC'
    ),
),

The only difference is the type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.