In my WordPress project, I have gotten some problem.
I have many categories. The categories have a child category name New product.
Currently, I have used pre_get_posts
to get the posts list in New product pages. If a post which have post_date
> 2018/1/15 will be get in the category pages.
I want to add a custom checkbox keep_product
in product which will keep product in New product page if post_date
< 2018/1/15 when it checked.
So I think the conditions should be post_date > 2018/1/15** OR **keep_product = 1
I try to create a query, but it seem WP_Query
only apply AND condition for date_query
and meta_query
.
function pre_get_posts_function($q){
//Code in here
$query->set('date_query', array(
array(
'after' => '2018/1/15'
);
));
$query->set('meta_query', array(
array(
'key' => 'keep_product',
'value' => '1'
)
));
}
I also try to create a posts_where
action, but It ignores category condition. The product which is checked keep_after_60_days
checkbox will be show in New product pages.
function pre_get_posts_function($q){
//Code in here
$query->set('date_query', array(
array(
'after' => '2018/1/15'
);
));
add_filter( 'posts_where', 'postmeta_new_releases',10,1);
}
function postmeta_new_releases( $where = '' ) {
global $wpdb;
$where .= " OR ($wpdb->postmeta.meta_key = 'keep_product' AND $wpdb->postmeta.meta_value = 1)";
return $where;
}
Could anyone help me. I get mad with it in some days.
Thanks!!