0

I have a meta field "a" with a1, a2, a11 multiplie values, etc.

get_post_meta(id, 'a'); displayed: Array array ([0] => a1 [1] => a6)

I do search by this code

array(
'key' => 'a',
'value' => 'a1',
'compare' => 'LIKE'
),

But there was a problem, LIKE does not exact search and coincidence. at a1 value records with the field a11 are also displayed. How it is possible to correct it that search was precisely on a1 to value?

'value' => 'a1',
'compare' => '='

And

'value' => array('a1'),
'compare' => 'IN'

don't work. nothing is displayed

Reynold
  • 41
  • 3
  • Possible duplicate of [WP\_Query search by meta\_key](https://stackoverflow.com/questions/44674948/wp-query-search-by-meta-key) – Ankur Bhadania Jun 26 '17 at 08:13

2 Answers2

1

i think this code woring.

 $args = array(
        'post_type'  => 'my_custom_post_type',
        'meta_key'   => 'age',
        'meta_query' => array(
            array(
                'key'     => 'age',
                'value'   => array( 3, 4 ),
                'compare' => 'IN',
            ),
        ),
    );
    $query = new WP_Query( $args );

Good Luck

0

Use custom query via get_results.

$querystr = "
    SELECT $wpdb->posts.* 
    FROM $wpdb->posts, $wpdb->postmeta
    WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id 
    AND $wpdb->postmeta.meta_key = 'a' 
    AND $wpdb->postmeta.meta_value = 'a1' 
    AND $wpdb->posts.post_status = 'publish' 
    AND $wpdb->posts.post_type = 'post'
    AND $wpdb->posts.post_date < NOW()
    ORDER BY $wpdb->posts.post_date DESC
 ";
 $pageposts = $wpdb->get_results($querystr, OBJECT);

https://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query

Bhunesh Satpada
  • 760
  • 1
  • 6
  • 18
  • how to transform this code in custom query? `$args = array( 'post_type' => 'product', 'posts_per_page' => $post_per_page, 'paged' => $paged, 'meta_key' => '_price', 'orderby' => $ordrby, 'order' => $order_date, 'product_cat' => $category_ID, 'meta_query' => array( array( 'key' => 'a', 'value' => $value_a, 'compare' => '=' ), array( 'key' => '_price', 'value' => array($price1, $price2), 'compare' => 'BETWEEN', 'type' => 'NUMERIC' ), ) ); }` – Reynold Jun 26 '17 at 09:25
  • and i can use this in wp_pagenavi args? now i use `$loop = new WP_Query( $args ); wp_pagenavi(array( 'query' => $loop ));` – Reynold Jun 26 '17 at 09:28
  • yes, but then it displays all posts. because this field exists everywhere – Reynold Jun 26 '17 at 09:58
  • **Array array ([0] => a1 [1] => a6)**. values are filled with a plugin "advanced custom fields pro". input type - multiplie select. – Reynold Jun 26 '17 at 11:05