I set following meta_query.
I like to add one more condition.
My problem is that the article which matches both meta_query conditions are double counted by count(get_posts on the end of line.
Is it possible to add condition like this? If both 1bb and 1aa matches, count it as if only 1bb matches.
<?php
$args = array(
'category_name' => $cat,
'posts_per_page' => -1,
'post_type' => 'post',
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'or',
array(
'key'=> '1bb',
'value' => array($from , $to),
'compare' => 'BETWEEN',
'type' => 'DATE',
),
array(
'key'=> '1aa',
'value' => array($from , $to),
'compare' => 'BETWEEN',
'type' => 'DATE',
),
),
);
echo count( get_posts( $args ) );
?>
Thank you.
The following is additional code that I tried to prevent from double counting.
Its too long time to load, and can not use it.
'meta_query' => array(
'relation' => 'or',
array(
'relation' => 'AND',
array(
array(
'key'=> '1aa',
'value' => array($from , $to),
'compare' => 'BETWEEN',
'inclusive' => 'true',
'type' => 'DATE',
),
array(
'key' => '1bb',
'compare' => 'NOT EXISTS',
),
),
),
array(
'relation' => 'AND',
array(
array(
'key'=> '1bb',
'value' => array($from , $to),
'compare' => 'BETWEEN',
'inclusive' => 'true',
'type' => 'DATE',
),
array(
'key' => '1aa',
'compare' => 'NOT EXISTS',
),
),
),
array(
'relation' => 'AND',
array(
array(
'key'=> '1bb',
'value' => array($from , $to),
'compare' => 'BETWEEN',
'inclusive' => 'true',
'type' => 'DATE',
),
array(
'key' => '1aa',
'compare' => 'EXISTS',
),
array(
'key' => '1bb',
'compare' => 'EXISTS',
),
),
),
),
Related
I'm trying to do the equivalent of this in meta_query but so far no success.
WHERE tm_url_1="" AND tm_url_2="" AND (tm_url_3="" OR tm_embed_code=!="") AND (tm_url_3!="" OR tm_embed_code=="")
Here the WP_Query. Once i add the OR clause nothing is returned.
$args = array(
'post_status' => $post_status,
'posts_per_page' => $posts_count,
'ignore_sticky_posts' => 1,
'post_type' => $post_type,
'order_by' => $order_by,
'order' => $order,
"meta_query" => array(
array('relation' => 'AND',
array(
'key' => 'tm_url_1',
'value' => '',
'compare' => '='
),
array(
'key' => 'tm_url_2',
'value' => '',
'compare' => '='
),
array(
'relation' => 'OR',
array(
'key' => 'tm_url_3',
'value' => '',
'compare' => '='
),
array(
'key' => 'tm_embed_code',
'value' => '',
'compare' => '!='
)
),
array(
'relation' => 'OR',
array(
'key' => 'tm_url_3',
'value' => '',
'compare' => '!='
),
array(
'key' => 'tm_embed_code',
'value' => '',
'compare' => '='
)
)
)
)
);
Nothing is returned. Where did i go wrong in the meta_query?
you should remove the first "array" in the media_query parameter:
"meta_query" =>
array(
'relation' => 'AND',
array(
'key' => 'tm_url_1',
'value' => '',
'compare' => '='
),
array(
'key' => 'tm_url_2',
'value' => '',
'compare' => '='
),
array(
'relation' => 'OR',
array(
'key' => 'tm_url_3',
'value' => '',
'compare' => '='
),
array(
'key' => 'tm_embed_code',
'value' => '',
'compare' => '!='
)
),
array(
'relation' => 'OR',
array(
'key' => 'tm_url_3',
'value' => '',
'compare' => '!='
),
array(
'key' => 'tm_embed_code',
'value' => '',
'compare' => '='
)
)
)
see: https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
Here is the correct way to add the meta_query for the conditions you want.
$args = array(
'post_status' => $post_status,
'posts_per_page' => $posts_count,
'ignore_sticky_posts' => 1,
'post_type' => $post_type,
'order_by' => $order_by,
'order' => $order,
'meta_query' => array(
array(
'key' => 'tm_url_1',
'value' => '',
) ,
array(
'key' => 'tm_url_2',
'value' => '',
) ,
array(
'relation' => 'OR',
array(
'key' => 'tm_url_3',
'value' => '',
) ,
array(
'key' => 'tm_embed_code',
'value' => '',
'compare' => '!='
)
) ,
array(
'relation' => 'OR',
array(
'key' => 'tm_url_3',
'value' => '',
'compare' => '!='
) ,
array(
'key' => 'tm_embed_code',
'value' => '',
)
)
)
);
Note that I have removed the default values for compare in some arrays assuming it is = as in your condition. The reference for this can be seen here.
I have custom post type having meta value include last update date.
I have to get post id in which meta value having latest date without compare other date.
below is my wp query to get post id but it is not working.
$args = array(
'posts_per_page' => 1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'post_status',
'compare' => '=',
'value' => 'active'
),
array(
'key' => 'other_cpt_id',
'compare' => '=',
'value' => $cptID
),
array(
'key' => 'post_last_update_date',
'value' => '',
'compare' => '>',
'type' => 'DATE'
)
),
'orderby' => array( 'meta_value_num' => 'ASC','ID' => 'ASC' ),
'post_type' => 'custom_post_type',
'post_status' => 'publish'
);
If you are using WP 4.2+, you may try the following:
$args = array(
'posts_per_page' => 1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'post_status',
'compare' => '=',
'value' => 'active'
),
array(
'key' => 'other_cpt_id',
'compare' => '=',
'value' => $cptID
),
array(
'key' => 'post_last_update_date',
'value' => '',
'compare' => '>',
'type' => 'DATE'
)
),
'orderby' => array( 'post_last_update_date' => 'ASC', 'ID' => 'ASC' ),
'post_type' => 'custom_post_type',
'post_status' => 'publish'
);
Some sources:
General WP_Query: https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters
4.0+ orderby array: https://make.wordpress.org/core/2014/08/29/a-more-powerful-order-by-in-wordpress-4-0/
4.2+ orderby meta data: https://make.wordpress.org/core/2015/03/30/query-improvements-in-wp-4-2-orderby-and-meta_query/
My Wordpress setup has a Publication custom post type and has 10 custom fields each corresponding to an author who may have contributed to that publication. Every post can have different authors in those fields or the same authors but in a different order. I am using Wordpress's WP_Query to search for posts matching my criteria. I need to check a single author name against all the custom fields to see if it matches any one of them. I tried doing this with the below code but it produces no result. Any help would be appreciated!
$args = array(
'post_type' => 'publication',
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'year_published',
'value' => $selected_pub_year,
'compare' => '=',
),
array(
'key' => array(
'author_0_name',
'author_1_name',
'author_2_name',
'author_3_name',
'author_4_name',
'author_5_name',
'author_6_name',
'author_7_name',
'author_8_name',
'author_9_name',
),
'value' => $selected_pub_author,
'compare' => '=',
),
),
);
// The above meta-query may be modified through the use of this:
// echo "<pre>".print_r($args[meta_query][relation])."</pre>";
// The Query
$the_query = new WP_Query($args);
I have added OR relation for post author query,Try to change your argument array as follow,
$args = array(
'post_type' => 'publication',
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'year_published',
'value' => $selected_pub_year,
'compare' => '=',
),
array(
'relation' => 'OR',
array(
'key' => 'author_0_name',
'value' => $selected_pub_author,
'compare' => '=',
),
array(
'key' => 'author_1_name',
'value' => $selected_pub_author,
'compare' => '=',
),
array(
'key' => 'author_2_name',
'value' => $selected_pub_author,
'compare' => '=',
),
array(
'key' => 'author_3_name',
'value' => $selected_pub_author,
'compare' => '=',
),
array(
'key' => 'author_4_name',
'value' => $selected_pub_author,
'compare' => '=',
),
array(
'key' => 'author_5_name',
'value' => $selected_pub_author,
'compare' => '=',
),
array(
'key' => 'author_6_name',
'value' => $selected_pub_author,
'compare' => '=',
),
array(
'key' => 'author_7_name',
'value' => $selected_pub_author,
'compare' => '=',
),
array(
'key' => 'author_8_name',
'value' => $selected_pub_author,
'compare' => '=',
),
array(
'key' => 'author_9_name',
'value' => $selected_pub_author,
'compare' => '=',
),
),
),
);
$the_query = new WP_Query( $args );
I've made a custom query containing a meta query but some fields are serialized arrays. Is there a way to get the value from that array? Everything I've tried so far won't work.
$query_args = array(
'post_type' => 'yacht',
'_meta_or_tax' => true,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'manufacturers',
'field' => 'id',
'terms' => $_GET['manufacturer']
),
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => $_GET['cat'],
)
),
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'yachts_length',
'value' => array($_GET['min_length'], $_GET['max_length']),
'compare' => 'BETWEEN',
'type' => 'NUMERIC',
),
array(
'key' => 'yachts_price',
'value' => array($_GET['min_price'], $_GET['max_price']),
'compare' => 'BETWEEN',
'type' => 'NUMERIC',
),
array(
'key' => 'yachts_year',
'value' => array($_GET['min_year'], $_GET['max_year']),
'compare' => 'BETWEEN',
'type' => 'NUMERIC',
),
array(
'key' => 'yachts_fuel',
'value' => $_GET['fuel'],
'compare' => 'LIKE',
),
array(
'key' => 'yachts_cabins',
'value' => $_GET['cabins'],
'compare' => 'LIKE',
'type' => 'NUMERIC',
),
array(
'key' => 'yachts_engine_type',
'value' => $_GET['engine_type'],
'compare' => 'LIKE',
),
array(
'key' => 'yachts_en',
'value' => $_GET['engines'],
'compare' => 'LIKE',
'type' => 'NUMERIC',
),
array(
'key' => 'yachts_drive',
'value' => $_GET['drive_type'],
'compare' => 'LIKE',
),
)
);
$yacht_query = new WP_Query( $query_args );
yachts_cabins, yachts_fuel, yachts_engine_type, yachts_en, yachts_type are serialized arrays.
I have a rather huge meta_query here and it hangs at execution: browser just won't load the page, it goes on loading ...
As far as I know this should work after WP 4.1 (I have it). Also tried updating to 4.2 to no avail.
Am I doing something terribly wrong here? Thanks for any info.
$meta_query = array(
'relation' => 'AND',
// IF $level GIVEN
array(
'relation' => 'OR',
array(
'key' => 'co_level',
'value' => $level,
'compare' => 'IN'
),
array(
'relation' => 'AND',
array(
'key' => 'co_program2',
'value' => $program_ids,
'compare' => 'IN'
),
array(
'key' => 'co_level2',
'value' => $level,
'compare' => 'IN'
),
),
array(
'relation' => 'AND',
array(
'key' => 'co_program3',
'value' => $program_ids,
'compare' => 'IN'
),
array(
'key' => 'co_level3',
'value' => $level,
'compare' => 'IN'
),
),
),
// IF $year GIVEN
array(
'relation' => 'OR',
array(
'key' => 'co_year',
'value' => $year,
'compare' => 'IN'
),
array(
'relation' => 'AND',
array(
'key' => 'co_program2',
'value' => $program_ids,
'compare' => 'IN'
),
array(
'key' => 'co_year2',
'value' => $year,
'compare' => 'IN'
),
),
array(
'relation' => 'AND',
array(
'key' => 'co_program3',
'value' => $program_ids,
'compare' => 'IN'
),
array(
'key' => 'co_year3',
'value' => $year,
'compare' => 'IN'
),
),
),
);
$args = array(
'post_type' => 'course',
'posts_per_page' => -1, // get all posts
'orderby' => array('meta_value_num'=>'ASC','menu_order'=>'ASC'), // order by given meta value AND menu_order
'meta_key' => $meta_key_orderby,
'order' => 'ASC',
'meta_query' => $meta_query,
);