user3625609
user3625609

Reputation: 31

meta_query not working in wp_query

My post type is product. I use a checkbox field with meta key is ht_featured, meta value when I print_r is array([0] => featured).
My WP_Query:

$the_query = new WP_Query(
    'post_type'     => 'product',
    'showposts'     => 12,
    'meta_query'    => array(
            array(
                    'key'       => 'ht_featured',
                    'value'     => array('featured'),
                    'compare'   => 'IN'
            )
    )
);

It doesn't show any post. I tried with value => 'featured' and 'compare' => 'EXISTS' but it not working.

Upvotes: 3

Views: 6450

Answers (4)

user3807767
user3807767

Reputation: 9

I had a similar problem until I used the function get_posts() rather than creating a new WP_Query. See if that helps...

Upvotes: 0

Razor Jack
Razor Jack

Reputation: 834

WP_query needs to be passed in an array. use following code and let me know if any prob.

$the_query = new WP_Query (array (
        'post_type'     => 'product',
        'showposts'     => 12,
        'meta_query' => array(
            array(
                'key'       => 'ht_featured',
                'value'     => array('featured'),
                'compare'   => 'IN'
            )
        )
    ));

You can refer to the discussion at wordpress forum:

http://wordpress.org/support/topic/how-to-wp_query-meta_query-value-string-contain-in-key-string

Upvotes: 5

Nathan Dawson
Nathan Dawson

Reputation: 19308

You're passing all of this into WP_Query as individual arguments when they should be contained in an array.

$the_query = new WP_Query( array( 
    'post_type'     => 'product',
    'showposts'     => 12,
    'meta_query'    => array(
        array(
            'key'       => 'ht_featured',
            'value'     => array('featured'),
            'compare'   => 'IN',
        ),
    ),
) );

Can you clarify your point about the checkbox? I'd suggest simply updating 'ht_featured' with either 'yes' or 'no' when you save the product. Then change your 'value' in the meta query to 'yes' and remove the 'compare'.

Upvotes: 1

MonkeyVoodoo
MonkeyVoodoo

Reputation: 538

Are you sure there is no php error? I think WP_Query needs to be passed in an Array

  $the_query = new WP_Query(
    array(
      'post_type'     => 'product',
      'showposts'     => 12,
      'meta_query'    => array(
              array(
                      'key'       => 'ht_featured',
                      'value'     => array('featured'),
                      'compare'   => 'IN'
              )
      )
  ));

Upvotes: 0

Related Questions