Support

Account

Home Forums General Issues Whats the value of a filled field in Query?

Solved

Whats the value of a filled field in Query?

  • Hi!

    I want to make a wp-query with a custom post type and just query the posts that contains a filled field from ACF.

    My exemplae is “customers” and if I fill out the field “Adress” the query should just post all my post types customers with the field adress filled.

    My example is <?php query_posts(array('post_type' => 'referenser', 'posts_per_page' => '2', 'orderby' => 'rand', 'meta_query' => array( array('key' => 'recension')))); while ( have_posts() ) : the_post(); ?>

    The key “recension” is a text field and I just want to make a query if this field is filled. And value => 1 is not working or value => true isn’t working either. So what should the value be? Should I even use value, or something totally diffrent?

  • Hi @Henric Åkesson

    If you head over to the docs http://codex.wordpress.org/Class_Reference/WP_Query and jump down to the “Custom Field Parameters” section, you will see that each meta_query array can have a ‘compare’ attribute. Just set this to “EXISTS” to test if the custom field value exists.

    Hope that helps.

    Thanks
    E

  • That doesn’t make any diffrence at all. Probably my fault because my code isn’t right, but could you give me a hint on my code down under:

    <?php query_posts(array('post_type' => 'referenser', 'posts_per_page' => '2', 'orderby' => 'rand', 'meta_query' => array( array('key' => 'recension', 'compare' => 'exists')))); while ( have_posts() ) : the_post(); ?>

    The field I want to check if it’s field is recension. If it’s not filled it should not query these posts.

  • Hi @Henric Åkesson

    I think ‘exists’ needs to be in capitals like ‘EXISTS’.

    Thanks
    E

  • Hi again!

    That dosen’t seem to make any diffrence, it just query all posts anyway. What I want is to query the posts that have written something in this field, not just that it contains it, because the field is not a “has to be filled-field”.

    So if the field is filled it should query these posts with something in it.

    What else should I try to do?

  • Hi @Henric Åkesson

    Thanks for the clarification.

    Perhaps your query should be:

    value != “”

    In the meta_query format, it would look like:

    
    array('key' => 'recension', 'value' => "", 'compare' => '!=')
    

    If that doesn’t work, perhaps you could debug the SQL which is run to see if you can spot an issue.

    There are plugins which can spit out all of the WP_Query debug data

    Thanks
    E

  • That’s just awesome! Thanks, finally. 🙂

Viewing 7 posts - 1 through 7 (of 7 total)

The topic ‘Whats the value of a filled field in Query?’ is closed to new replies.