0

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()

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.

  • The custom field is different and i know how to show them, but my question is related to getting fields to fill in meta_value => 'somename' how do i find it, if i am using woocommerce. Like i want to create a query to display shop items, like archive-page does.Like a custom query. The actions defined in archive page doesn't work if i copy and paste them in my template. Like take a example of loop that displays all data from shop,plus if i include and want to display my attr. like awesomescreenshot.com/00c2418te2 – Nofel Dec 18 '13 at 18:37
  • I have to go for now, but one thing I have to say real quick, you know attributes are a taxonomy? They aren't post meta. – Nicolai Dec 18 '13 at 18:41
  • that's clear. But how i display those taxonomy as attributes. Can i reuse actions that are in archive page? I want to display the same product loop in one of my pages. – Nofel Dec 18 '13 at 20:10
  • Sorry, but I've hard time getting what you want. I add an edit to my answer to hint you in right direction - or what I think is the right direction. @Nofel – Nicolai Dec 18 '13 at 23:49
  • When you mention this, "For getting just keys use get_post_custom_keys() and for values there is get_post_custom_values() available." How do i use it? Is using custom_meta is starting with _ in code or do i also add a prefix of pa_ – Nofel Jan 21 '14 at 5:44
-1

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();
  • Please add details to your answer explaining what it does – Maruti Mohanty Dec 18 '13 at 19:28

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.