How to find meta_values to call

The question:

This thing has really confused me, when writting a custom query with wp_query we have pass multiple arguments. But i am using woocommerce, and i found this part of query on some form, which is

$args = array(  
'post_type' => 'product',  
'meta_key' => '_featured',  
'meta_value' => 'yes',  
'posts_per_page' => 10  

My question is, how to find out, the meta_key, do i look into database? or backend? suppose i want to find out about a product information, lets say how many grams or lets says colors of a shirt. How and where do i look for it, to pass it along with meta_key? Do i look into database or where? and after i get the meta_key, how do i display it in the loop, like we say the_title()

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

On the post edit screen you can have the »Custom Fields« meta box, looking somewhat like shown here. You might have to enable it via the screen options. This way you can see the meta data for the edited post, but underscored meta data won’t be shown, because it is hidden. To make the hidden meta visible during development put this into your functions.php:

    function unprotected_meta( $protected, $meta_key ) {
        $protected = ( '-' == $meta_key[0] );
        return $protected;
    }
    add_filter( 'is_protected_meta', 'unprotected_meta', 10, 2 );

As for a function you can use to get all the post meta, there is get_post_custom(), which

Returns a multidimensional array with all custom fields of a particular post or page.

For getting just keys use get_post_custom_keys() and for values there is get_post_custom_values() available.


Edit: in response to comment

If you want to query for taxonomies you should take a look into

As you’re talking about archives you might want to take a look at templates

because you can have one for taxonomies.

Product attributes are saved by woocommerce as taxonomy, thereby prefixing the taxonomy name with pa_, e.g. pa_room-size. If you want to get the attributes for a product you could it via post meta:

$product_attributes_pm = get_post_meta( get_the_ID(), '_product_attributes' );
echo '<pre>';
print_r($product_attributes_pm);
echo '</pre>';

Or you use the woocommerce function get_attributes():

global $product;
$product_attributes_wcf = $product->get_attributes();
echo '<pre>';
print_r($product_attributes_wcf);
echo '</pre>';

Or you can get the information directly from the $product object:

global $product;
$product_attributes_obj = maybe_unserialize( array_shift( $product->product_custom_fields['_product_attributes'] ) );
echo '<pre>';
print_r($product_attributes_obj);
echo '</pre>';

Hope that helps and gives you a little bit deeper insight on how to proceed in your case.

Method 2

Use this

$args = array(  
'post_type' => 'product', 
'posts_per_page' => 10 
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : 
  setup_postdata( $post ); ?>
  $custom_fields = get_post_custom($post->ID);
  $my_custom_field = $custom_fields['_featured'];
  foreach ( $my_custom_field as $key => $value ) {
    echo $key . " => " . $value . "<br />";
  }
endforeach;
wp_reset_postdata();


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment