WordPress

Discussion in 'Programming' started by Robin Mohammad, Sep 25, 2014.

  1. #1
    I'm currently working on a widget which returns a single random featured property on a website but I'm having some trouble with my WP_Query arguments.

    In the backend I have 2 checkbox meta fields "featured" and "hidden". I want to return a post which is has the "featured" field checked and the "hidden" field unchecked. My query is as follows:

    $query_args = array('posts_per_page'=>'1','orderby'=>'rand','post_type'=>'nc_property','meta_query'=> array(
    array('key'=>'nc_featured','value'=>'on','compare'=>'='),
    array('key'=>'nc_hidden','value'=>'on','compare'=>'!=')));
    The query returns a random featured property if I remove the second array so the problem lies with my nc_hidden meta query but I'm not seeing it.

    What's the correct way to check for an unchecked meta value?
     
    Robin Mohammad, Sep 25, 2014 IP
  2. Shibbir Talukder

    Shibbir Talukder Greenhorn

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #2
    The problem was that the nc_hidden met property did not exist if it was not checked so there was essentially no value to query against. I swapped nc_hidden from a checkbox to a select and updated my query as required. The working query is now as follows: $query_args = array( 'posts_per_page' => '1', 'orderby' => 'rand', 'post_type' => 'nc_property', 'meta_query' => array( array( 'key' => 'nc_featured', 'value' => 'on', 'compare' => '=' ), array( 'key' => 'nc_hidden', 'value' => 'Visible', 'compare' => '=' ) ) ); Thanks @milo for pointing me in the right direction.
     
    Shibbir Talukder, Sep 25, 2014 IP