How to use meta_query with multiple parameters?


I'm making a template with two queries. The first query shows 3 featured items and the second query shows 9 basic items.

Now, the template is build in such a way that when the time set for an item is exceeded, then it will no longer show on the front end. That work's great, however, I need to add another meta_query so only items with the featured (value = 2 ) option selected under code_type will appear.

How do I do that? Please see my query below (note the bold part). Everything works great, except the section that is in bold. I have tried to add compare = "IN" and "=" but nothing seems to be working.

$args = array(
	'post_type'		=> 'code',
	'post_status'	=> 'publish',
	'posts_per_page'=> 3,
	'meta_key'		=> 'code_expire',
	'orderby'		=> 'rand',
	'order'			=> 'ASC',
	'paged' 		=> $cur_page,
	'meta_query'	=> array(
		'relation'	=> "AND",
			'key' => 'code_type',
			'value'	=> 2, // 2 is the option for "Featured"
			'compare'	=> '='</strong>
			'key' => 'code_expire',
			'value' => time() + 86400 * $_GET['start_range'],
			'compare' => '>='
			'key' => 'code_expire',
			'value'	=> time() + 86400 * $_GET['end_range'],
			'compare'	=> '<='
	'tax_query' => array(
            'taxonomy' => 'code_category',
            'field' => 'slug',
            'terms' => getCodeCategories(array('konkurrencer','kun-for-medlemmer')),
            'operator' => 'IN'
  • Vaughan

    Hi Mika,

    Hope you're well?

    This might be byond my expertise, but the above looks ok to me.

    Have you tried parsing the query vars and then viewing the generated SQL from the above to see what it looks like?

    $meta_query->parse_query_vars( $args );
    $mq_sql = $meta_query->get_sql(
    	$context = null

    List of Arguments:

    $type' (string) - Type of meta(post, comment, user).
    $primary_table (string) - The table where we will be looking for rows. You can pass for instance $wpdb->posts, or the alias name if you are aliasing the table in your SQL query
    $primary_id_column (string) - The column that holds the ID(for posts, it is ID, for comments it is comment_ID and for users is ID).
    $context (object) - Optional - The main query object. It is only used when the result is passed to the get_meta_sql filter.
    On failure(for instance - lacking any meta query parameters in the query array), this function will return false.

    But let me just ask one of our developers for a hand with this, as i'm not entirely sure on this myself.

    Hope this helps

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.