Date range find in wordpress - php

In wordpress I have only publish_date(not meta_key) and meta_key "end_date"
Example: event from 2015-06-22(publish_date) to 2015-08-24(meta_ket = "end_date")
now I want search all events
(from 2015-07-05 to 2015-07-24)
or
(from 2015-08-05 to 2015-09-25)
and must show event in example
This code not work:
$opt = array(
'post_type' => 'event_info',
'posts_per_page' => '40',
'paged' => $page_num,
'orderby' => 'post_date ID',
'order' => 'DESC',
'relation' => 'OR',
'meta_query' => array(
'key' => 'end_date',
'value' => array('$date_start','$date_end'),
'compare' => 'BETWEEN',
'type' => 'numeric'),
'date_query' => array(
array(
......
'compare' => 'BETWEEN',
),
),
)
);
relation between meta key and data_query not work,
current idea is create 2 query but I dont want do this

try this : here I added timestamp in startdate and enadate meta key
$chkdt = current_time('timestamp');
$args = array('post_type'=>'event_info', 'post_status' => 'publish','posts_per_page'=>-1,'orderby' => 'ID','order' => 'DESC', 'meta_query' =>
array(
'relation' => 'AND',
/******filter start date and End date****************/
array(
'key' => 'end_date',
'value' => $chkdt,
'compare' => '>=',
'type' => 'NUMERIC',
),
array(
'key' => 'start_date',
'value' => $chkdt,
'compare' => 'NUMERIC',
),
/******END filter start date and End date****************/
),
);

Related

WordPress Query order by two keys in dependency

I sort a query by a customfield start_date (form type Ymd). The sorting should always be descending from new to old. If there are multiple entries for the date on one day, the entries should be sorted by the order of publication.
I tried it with the (post) date, ID, menu_order but it never works in all cases.
The start_date may be a different day than the post date.
Is there any query way?
Or do I have to add hours and minutes to the news_start_date field?
Or maybe switch to the post date over all, but this means in this backend flow a big lost of elegance.
$meta_query = array(
'relation' => 'AND',
array(
'key' => 'news_start_date',
'value' => $today,
'compare' => '<=',
'type' => 'DATE'
),
array(
'relation' => 'OR',
array (
'relation' => 'AND',
array(
'key' => 'news_start_date',
'value' => $expire_date,
'compare' => '>=',
'type' => 'DATE'
),
array(
'key' => 'news_end_date',
'compare' => 'NOT EXISTS'
),
),
array (
'relation' => 'AND',
array(
'key' => 'news_end_date',
'compare' => 'EXISTS'
),
array(
'key' => 'news_end_date',
'value' => $today,
'compare' => '>=',
'type' => 'DATE'
),
),
),
);
$args = array(
'post_type' => 'news',
'meta_query' => $meta_query,
'posts_per_page' => $news_count,
'meta_key' => 'news_start_date',
'orderby' => 'meta_value',
'orderby' => array(
'meta_value' => 'DESC',
'ID' => 'DESC',
//'menu_order' => 'DESC',
//'date' => 'DESC',
//'publish_date' => 'DESC',
),
'order' => 'DESC',
'post__not_in' => $sticky_posts,
'ignore_sticky_posts' => true,
'cache_results' => true,
'update_post_meta_cache' => true,
'no_found_rows' => true,
'suppress_filters' => false,
);
I tried the others fields but there was never a 100% correct order

PHP Wildcard to display all values of variable

We have set up custom post types for real estate properties and a search form to search through these custom post types using price, city, newest, etc. We are using query variable strings/if statements to display the results, but we are having trouble with the sort options. We have three sort options in place : Newest, Price Low-High, and Price High-Low.
When the default results are displayed, the sort does not work correctly, but when a city is set in the search dropdown, the sort works perfectly.
For the city search, we have this code in place:
if (isset($_POST['city'])) {
$qs_city = get_query_var('city');
}
else {
$qs_city = " ";
I believe that this is due to the else portion of the code above. We need some sort of wildcard that allows all cities listed to be listed if a city is not set. We have tried several different things, but nothing seems to work. I'm fairly new to this, so I can better clarify any of the above, if necessary.
This is the code used to sort:
if (isset($_POST['sort'])) {
$qs_sort = get_query_var('sort');
switch ($qs_sort) {
case 'newest':
$args = array(
'post_type' => 'residential',
'posts_per_page' => 10,
'paged' => $paged,
'orderby' => 'L_StatusDate',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'L_City',
'value' => $qs_city,
'compare' => 'LIKE'
),
array(
'key' => 'L_SystemPrice',
'value' => array( $qs_price_min, $qs_price_max ),
'type' => 'numeric',
'compare' => 'BETWEEN'
),
array(
'key' => 'LM_Int1_2',
'value' => $qs_beds,
'compare' => '>='
),
array(
'key' => 'LM_Int1_3',
'value' => $qs_baths,
'compare' => '>='
)
)
);
break;
case 'price-low':
$args = array(
'post_type' => 'residential',
'posts_per_page' => 10,
'paged' => $paged,
'orderby' => 'L_SystemPrice',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'L_City',
'value' => $qs_city,
'compare' => 'LIKE'
),
array(
'key' => 'L_SystemPrice',
'value' => array( $qs_price_min, $qs_price_max ),
'type' => 'numeric',
'compare' => 'BETWEEN'
),
array(
'key' => 'LM_Int1_2',
'value' => $qs_beds,
'compare' => '>='
),
array(
'key' => 'LM_Int1_3',
'value' => $qs_baths,
'compare' => '>='
)
)
);
break;
case 'price-high':
$args = array(
'post_type' => 'residential',
'posts_per_page' => 10,
'paged' => $paged,
'orderby' => 'L_SystemPrice',
'order' => 'DESC',
'meta_query' => array(
//'relation' => 'AND',
array(
'key' => 'L_City',
'value' => $qs_city,
'compare' => 'LIKE'
),
array(
'key' => 'L_SystemPrice',
'value' => array( $qs_price_min, $qs_price_max ),
'type' => 'numeric',
'compare' => 'BETWEEN'
),
array(
'key' => 'LM_Int1_2',
'value' => $qs_beds,
'compare' => '>='
),
array(
'key' => 'LM_Int1_3',
'value' => $qs_baths,
'compare' => '>='
)
)
);
break;
}
}
else {
$args = array(
'post_type' => 'residential',
'posts_per_page' => 10,
'paged' => $paged,
'meta_query' => array(
//'relation' => 'AND',
array(
'key' => 'L_StatusDate',
'orderby' => 'DESC'
//set default to sort by newest
),
array(
'key' => 'L_City',
'value' => $qs_city,
'compare' => 'LIKE'
),
array(
'key' => 'L_SystemPrice',
'value' => array( $qs_price_min, $qs_price_max ),
'type' => 'numeric',
'compare' => 'BETWEEN'
),
array(
'key' => 'LM_Int1_2',
'value' => $qs_beds,
'compare' => '>='
),
array(
'key' => 'LM_Int1_3',
'value' => $qs_baths,
'compare' => '>='
)
)
);
}
?>
The main question for your situation is "what kind of application will sort the results?". It looks like you use some kind on database. But which one? If it's SQL-like database, - just replace $qs_city = " " with $qs_city = "%";

how to display popular posts by multiple meta keys in wordpress?

I want to display popular post in custom page , I have added post views and post like option in wordpress and I want to show popular posts based views and likes,
I tried this code but it's ranking only for views counter ;
$args = array(
'posts_per_page'=>5,
'orderby' => 'meta_value',
'order' => 'DESC',
'meta_query' => array(
'relation' => 'AND',
'_post_views' => array(
'key' => '_post_views',
'type' => 'NUMERIC',
'compare' => 'LIKE'
),
'_post_like_count' => array(
'key' => '_post_like_count',
'type' => 'NUMERIC',
'compare' => 'LIKE'
),
),
);
thanks for answers
Try new orderby feature:
$args = array(
'posts_per_page' => 5,
'meta_query' => array(
'relation' => 'AND',
'_post_views' => array(
'key' => '_post_views',
'type' => 'NUMERIC',
'compare' => 'LIKE'
),
'_post_like_count' => array(
'key' => '_post_like_count',
'type' => 'NUMERIC',
'compare' => 'LIKE'
),
),
'orderby' => array(
'_post_views' => 'DESC',
'_post_like_count' => 'DESC'
)
);

WP_Query, using AND with OR

I have the below PHP:-
$args = array(
'posts_per_page'=> -1,
'post_type' => 'jobs',
'order' => 'ASC',
's' => $search_field,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'job_salary_from',
'value' => $job_salary_from,
'compare' => '>=',
),
array(
'key' => 'job_salary_from',
'value' => $job_salary_to,
'compare' => '<=',
),
array(
'relation' => 'OR',
array(
'key' => 'job_salary_to',
'value' => $job_salary_from,
'compare' => '>=',
),
array(
'key' => 'job_salary_to',
'value' => $job_salary_to,
'compare' => '<=',
),
))
);
Lets say there is a job that is from 19000 to 22000 called 'ABC', so
job_salary_from = 19000
and job_salary_to = 22000.
Now lets say I search a job that is between 10000 and 19000, so $job_salary_from = 10000 and $job_salary_to = 19999.
This shows up when I do a search which is correct. However, if I search for a job that is between 20000 and 29999, so $job_salary_from = 19999 and $job_salary_to = 29999 nothing is showing up.
Where as the job 'ABC' should show up because $job_salary_to is within the search bracket.
Any help would be much appreciated.
I think the meta_query syntax is not quite right. Try this:
$args = array(
'posts_per_page'=> -1,
'post_type' => 'jobs',
'order' => 'ASC',
's' => $search_field,
'meta_query' => array(
'relation' => 'OR',
array(
'relation' => 'AND',
array(
'key' => 'job_salary_to',
'value' => $job_salary_from,
'type' => 'numeric', //for each case
'compare' => '>=',
),
array(
'key' => 'job_salary_to',
'value' => $job_salary_to,
'type' => 'numeric',
'compare' => '<=',
),
),
array(
'relation' => 'AND',
array(
'key' => 'job_salary_from',
'value' => $job_salary_from,
'type' => 'numeric',
'compare' => '>=',
),
array(
'key' => 'job_salary_from',
'value' => $job_salary_to,
'type' => 'numeric',
'compare' => '<=',
),
)
)
);
Also you can try to use BETWEEN, even I am not sure if this is inclusive or exclusive compare.
that is because there is a different function to fetch results in between a range.
Try the following example to fetch results in between a range
$args = array(
'post_type' => 'product',
'meta_query' => array(
array(
'key' => 'color',
'value' => 'blue',
'compare' => 'NOT LIKE',
),
array(
'key' => 'price',
'value' => array( 20, 100 ),
'type' => 'numeric',
'compare' => 'BETWEEN',
),
),
);
$query = new WP_Query( $args );
More resources here https://codex.wordpress.org/Class_Reference/WP_Query

WordPress Meta-Query between 2 dates

I have currently a small problem understanding with meta-queries by WordPress. Initial situation:
A Custom Post Type with 2 meta-Fields (offer_start-date, offer_end-date) The CPT is intended as an offer, which should be displayed in the specified time period (between start-date and end-date) . The date here is formatted in german format DD.MM.YYYY. For that I use currently following query:
$args = array(
'post_type' => 'offer',
'posts_per_page' => -1,
'post_status' => 'publish',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'offer_start-date',
'value' => date( 'd.m.Y', time() ),
'type' => 'numeric',
'compare' => '<='
),
array(
'key' => 'offer_end-date',
'value' => date( 'd.m.Y', time() ),
'type' => 'numeric',
'compare' => '>='
)
)
);
new WP_Query( $args );
Unfortunately, the query does not yield reliable results. I can not even say 100% why. On some days all offers appear, on other days there are no offers.
I have also tried to find out the cause of the problem in the Codex, but it seems I am a strong blockhead.
If you need tu use between just use like that:
'meta_query' => array(
array(
'key' => 'event_date',
'value' => array(date('d/m/Y'), date('d/m/Y', strtotime('28 days'))),
'compare' => 'BETWEEN',
'type' => 'DATE'
),
)
This below solution may be handy to someone
$args = array(
'cat' => $cat_ID,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'date_from',
'value' => date("Y-m-d H:i:s"),
'compare' => '<=',
'type' => 'DATE'
),
array(
'key' => 'date_to',
'value' => date("Y-m-d H:i:s"),
'compare' => '>=',
'type' => 'DATE'
)
),
'orderby' => 'date',
'order' => 'DESC'
);

Categories