5

I want do a custom query to get all products with an especific attribute ("demo" in my case) The query what i want returns from this:

a:5:{s:4:"demo";
a:6:{s:4:"name";
    s:4:"DEMO";
    s:5:"value";
    s:366:"LINK TO DEMO";
    s:8:"position";
    s:1:"0";
    s:10:"is_visible";
    i:0;
    s:12:"is_variation";
    i:0;
    s:11:"is_taxonomy";
    i:0;
}

}

I don't know how do an $args to get products. I want $args be something like this:

    $args = array ( 
         'meta_query' => array( 
             array( 
              'key' => 'meta_value', 
              'value' => 'demo', 
              'compare' => 'LIKE', ), 
           ), 
   );

Thanks!

3

OK I HAVE ALREADY!! yuhuuu!

Thanks a lot guys!!

I have this:

$args = array ( 
     'post_type'  => 'product',
     'posts_per_page'  => -1,
     'meta_query' => array( 
         array( 
          'value' => 'demo', 
          'compare' => 'like'
         ), 
       ),
     ); 

Well this works at least for me

Thanks thanks!!

Best regards!

1
  • Just for someone looking similar solution, this code will not work for most of the cases. It might have worked for specific situation. – Yatix Aug 17 '16 at 6:26
2

You have written key as meta_value. It should be your meta name. The name you have given to your custom fields or meta. Then use the following query.

$args = array ( 
         'post_type'  => 'your-post-type',
         'posts_per_page'  => -1,
         'meta_query' => array( 
             array( 
              'key' => 'demo', 
              'value' => '',
              'compare' => '!='
             ), 
           ), 
   );

By default the compare is set to =

6
  • Thanks, i want get all products if have an attribute called 'demo', i wont get value because value's field will be a link Anyways, thanks! – Cristian B. Jun 29 '15 at 8:37
  • @user2879109 I updated the code. Please check – Karun Jun 29 '15 at 8:49
  • No, i tried and doesn't work for me. In my wp_postmeta.meta_value i have this: a:5:{ s:4:"demo"; a:6:{s:4:"name"; s:4:"DEMO"; s:5:"value"; s:366:"LINK TO DEMO"; s:8:"position"; s:1:"0"; s:10:"is_visible"; i:0; s:12:"is_variation"; i:0; s:11:"is_taxonomy"; i:0; } ... more attributes } – Cristian B. Jun 29 '15 at 9:06
  • in any of your post where you have that demo key. Can you do a get_post_meta(get_the_ID()); and show the returned array. – Karun Jun 29 '15 at 9:14
  • I got it, thanks for your help!! i wrote the code below – Cristian B. Jun 29 '15 at 9:20
1

try something like this

$args = array(
    "post_type" => "product",
    "meta_query" => array(
        array(
            "key"     => "demo",
            "value" => "abc",
            "compare" => "EXISTS",
        ),
    ),
);
$query = new WP_Query( $args );
4
  • Thanks but im still getting 0 results – Cristian B. Jun 29 '15 at 8:36
  • add "value" => "abc", next to "key" and "compare". the doc speak about a possible bug but it's not clear for me if you need "value" or not (I modified the code above) – mmm Jun 29 '15 at 9:02
  • I got it, your code and karun's code help so much! Thanks. – Cristian B. Jun 29 '15 at 9:19
  • meta_key that stores woocommerce attributes is '_product_attributes'. Still there is no specific way to search product based on custom product attributes. – Yatix Aug 17 '16 at 6:31
1

I know this is old, but straight sql, here are my solutions:

Get all meta data for a product:

SELECT meta_key, meta_value FROM wp_postmeta WHERE wp_postmeta.post_id = 626

Get products with specific meta data

SELECT p.id, p.post_title, m.meta_key, m.meta_value FROM wp_posts p 
INNER JOIN wp_postmeta m ON p.id=m.post_id AND m.meta_key='_auction_dates_to' 

APPEND TO GET META DATA FOR A SPECIFIC PRODUCT

AND p.id=626

Append to get specific meta value

AND m.meta_value='today'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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