How to create a meta_query to get all posts with a specific meta data?

admin2025-01-08  7

Here are the args for my query :

$args = array(
        'post_type' => 'product',
        'posts_per_page' => -1,
        'meta_query' => array(
            array(
                'key' => 'vendor',
                'value' => 'farsi',
                'compare' => '=',
            ),
    )
    );

This doesn't work when topics is a string, it gets me all products, But I need to get all products that have meta data (vendor) farsi.

UPDATE:

i use this code that move out of stock products to last product in archive:

add_action( 'pre_get_posts', 'move_out_of_stock_products_to_end' );

When stop it, the query work good.

So what can do !!

Here are the args for my query :

$args = array(
        'post_type' => 'product',
        'posts_per_page' => -1,
        'meta_query' => array(
            array(
                'key' => 'vendor',
                'value' => 'farsi',
                'compare' => '=',
            ),
    )
    );

This doesn't work when topics is a string, it gets me all products, But I need to get all products that have meta data (vendor) farsi.

UPDATE:

i use this code that move out of stock products to last product in archive:

add_action( 'pre_get_posts', 'move_out_of_stock_products_to_end' );

When stop it, the query work good.

So what can do !!

Share Improve this question edited Jun 15, 2017 at 10:11 Johen asked Jun 14, 2017 at 14:10 JohenJohen 114 bronze badges 2
  • 1 Are you sure vendor is the meta_key and not wpcf-vendor? Please check in database and confirm. – Jiten Gaikwad Commented Jun 14, 2017 at 15:19
  • yes i check it in database – Johen Commented Jun 15, 2017 at 7:03
Add a comment  | 

2 Answers 2

Reset to default 1

If you want to target meta key vendor and meta value farsi, you need to pass the following meta_query arguments:

$args = array(
    'posts_per_page' => -1,
    'post_type' => 'product',
    'meta_query' => array(
        array(
            'key'     => 'vendor',
            'value'   => 'farsi',
        )
    )
);

Also to note, as Jiten highlighted, your meta key may be wpcf-vendor.

Please use the code and let me know

$args = array( 
    'post_type' => 'book',
    'meta_key' => 'author',
    'meta_value' => 'Don',
    'meta_compare' => '=',
    'post_status' => 'publish'
);
$query = new WP_Query( $args );
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736270697a1448.html

最新回复(0)