Wordpress: Query Year in a Range of years - wordpress

I have a start_year and a end_year
f.e: Project-Lorem-Ipsum: $start_year=2001, $end_year=2005
and I need to select a certain year (f.e. $filter_year=2002) and show the posts that are running in this year.
That means, $filter_year could be $start_year or $end_year or between $start_year and $end_year.
I did this sketch to show maybe better what i need,
thanks for any help!
$filter_year=2002;
$posts = get_posts(array(
'posts_per_page' => -1,
'post_type' => 'post',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'start_year',
'value' => $filter_year,
'compare' => '???',
),
array(
'key' => 'end_year',
'value' => $filter_year,
'compare' => '???',
),
),

You can use meta query for this. But there are changes to be made to make these work, if not already implemented.
1: You can use the date type in meta query but it will only work in the yyyy-mm-dd format
$start_date = date('Y-m-d', strtotime('1/1/15') ); // cant just use the year, it will see it as a time value
$end_date = date('Y-m-d', strtotime('31/12/15') ); // cant just use the year, it will see it as a time value
'meta_query' => array(
'relation' => 'AND'
array(
'key' => 'start_year',
'value' => $start_date,
'compare' => '>='
),
array(
'key' => 'start_year',
'value' => $end_date,
'compare' => '<='
),
)
2: Convert dates to timestamps (preferred)
$start_date = strtotime('1/1/15'); // cant just use the year, it will see it as a time value
$end_date = strtotime('31/12/15'); // cant just use the year, it will see it as a time value
'meta_query' => array(
'relation' => 'AND'
array(
'key' => 'start_year',
'value' => $start_date,
'compare' => '>='
),
array(
'key' => 'start_year',
'value' => $end_date,
'compare' => '<='
),
)
You can loop over the meta values using wp_query or get_posts and checking to see if the get_post_meta($postid, 'start_year', true); exists.

Related

Wordpress meta_query of certain days after date stored in custom field

I like to set meta_query for 10 days after date stored in custom field.
I set like this, but it does not work.
Meta key '2a' has value like '2022-02-15'.
<?php
$today = wp_date('Y-m-d');
$args = array(
'meta_query' => array(
'relation' => 'AND',
array(
'key'=> '2a',
'value' => array( date("Y-m-d", strtotime("+10 day", strtotime(get_post_meta($post->ID , '2a' ,true)))), $today ),
'compare' => '<=',
'type' => 'DATE',
),
array(
'key'=> '3a',
'compare' => 'EXISTS'
),
));?>
You're passing two values into the date portion of your query which suggests you need to compare between as opposed to <=.
I tend to find it's better to perform calculations first and then pass the result into the query. Merging it all together as in your example makes it much harder to follow.
While strtotime() will work, there are classes that make handling time easier and more readable.
Example:
// Keeping the code brief for the example but you'll probably want to do some checks here.
$twoADate = new DateTimeImmutable( get_post_meta( $post->ID , '2a' ,true ) );
$tenDaysLater = $twoADate->modify( '+10 days' )->format( 'Y-m-d' );
$today = wp_date( 'Y-m-d' );
$args = [
'meta_query' => [
'relation' => 'AND',
[
'key'=> '2a',
'value' => [$tenDaysLater, $today],
'compare' => 'BETWEEN',
'type' => 'DATE',
],
[
'key' => '3a',
'compare' => 'EXISTS'
],
],
];
$today = wp_date('Y-m-d');
$TenDaysBefore = date("Y-m-d", strtotime("-10 day", strtotime($today)));
'key'=> '2a',
'value' => $TenDaysBefore ,
'compare' => '<=',
'type' => 'DATE',
Works now

WP_Query - Load delay with multiple meta_query

I have a Wordpress site that uses quite a few filters to display jobs.
The query will filter jobs by:-
The Job Type
The Location
Job Sector
If it is an International Job
Will check the salary between two values
The current query I am running is:-
<?php
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
'posts_per_page'=> -1,
'post_type' => 'jobs',
'order' => 'DESC',
'posts_per_page' => 20,
'paged' => $paged,
's' => $search_field,
'meta_query' => array(
'relation' => 'AND',
array(
array(
'key' => 'job_type',
'value' => $job_type,
'compare' => 'LIKE',
),
array(
'key' => 'job_location',
'value' => $job_location,
'compare' => 'LIKE',
),
array(
'key' => 'job_sector',
'value' => $job_sector,
'compare' => 'LIKE',
),
array(
'key' => 'international_job',
'value' => $international_job,
'type' => 'numeric',
'compare' => 'IN',
),
array(
'key' => 'job_location',
'value' => $location_array,
'compare' => 'NOT IN',
),
),
array(
'relation' => 'OR',
array(
'relation' => 'AND',
array(
'key' => 'anual_to_salary',
'value' => array($job_salary_from,$job_salary_to),
'type' => 'numeric',
'compare' => 'BETWEEN',
),
),
array(
'relation' => 'AND',
array(
'key' => 'anual_from_salary',
'value' => array($job_salary_from,$job_salary_to),
'type' => 'numeric',
'compare' => 'BETWEEN',
),
),
array(
'relation' => 'OR',
array(
'key' => 'job_negotiable',
'value' => 'yes',
'compare' => '=',
),
),
),
)
);
$fetch_jobs = new WP_Query( $args );?>
I'm assuming this is a bad way to do this kind of query as it's taking around 5 - 10 seconds to display any results.
Can anyone recommend a better way to do this type of query with multiple filters?
Thanks in advance.
First of all Your code is like a charm, there is no better way to execute wp_query();. it is taking 5 to 10 second because there is lot of conditions to check and it totally depends on the data on which it have to perform filtration.
I saw whole code and just need a little change that you pass same argument twice i.e. posts_per_page please correct it. else it is the best to perform wp_query.

Wp_query with 2 meta keys and array of meta values

Hi in my post_type=shop i have 2 meta keys and array of values
Custom fields
Name Values
cu_status pending,processing,completed
cu_date 12-Jan-2016 , 13-Jan-2016, ...... any date in the same format date("d-M-Y")
Now i need to loop through all posts with cu_status =pending,processing and cu_date is between 12-Jan-2016 to 13-Apr-2016
What will the query ?
Iam very confused . For to get all post with status pending,processing I know the query
$args = array(
'post_type' => 'shop',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'cu_status',
'value' => array('pending','processing'),
'compare' => 'IN',
)
),
'posts_per_page' => -1
);
Pleases help to complete the query .
You need to use the relation operator e.g.
$args = array(
'post_type' => 'shop',
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'cu_status',
'value' => array('pending','processing'),
'compare' => 'IN',
) ,
array(
'key' => 'cu_date',
'value' => array($start, $end),
'compare' => 'BETWEEN',
'type' => 'DATE'
)
),
'posts_per_page' => -1
);
Also use the compare=>BETWEEN for getting the difference in 2 dates.
You may need to tweak the code a bit as I have not tested it.

Multiple values in meta_key query

I have a variable that looks like this
$the_vacancies_industry_areas_list = 'Call Centre','Child Care','Cleaning'
And i want to find if just one of these variables satisfies this case in a meta query
array(
'key' => 'industry_areas_list',
'value' => $the_vacancies_industry_areas_list,
'compare' => 'LIKE',
),
It only seems to satisfy if all three areas are present, not just one.
I want someone with just 'Child Care' or 'Cleaning' for example.
You need to set the relation type on the wp_query args to OR, then define each of the OR conditions as arrays.
$args = array(
'meta_query' => array(
'relation' => 'OR',
array (
'key' => 'industry_areas_list',
'value' => 'Call Centre',
'compare' => 'LIKE'
),
array (
'key' => 'industry_areas_list',
'value' => 'Child Care',
'compare' => 'LIKE'
),
array (
'key' => 'industry_areas_list',
'value' => 'Cleaning',
'compare' => 'LIKE'
)
);
Try this: I made $the_vacancies_industry_areas_list as array variable and pass into wp_query
$the_vacancies_industry_areas_list = array('Call Centre','Child Care','Cleaning');
$args = array(
'post_type' => 'YOUR_POST_TYPE', //Set your post_type
'post_status' => 'publish', //Set your post_status
'meta_query' => array(
array(
'key' => 'industry_areas_list',
'value' => $the_vacancies_industry_areas_list,
'compare' => 'IN'
),
),
);

wordpress custom query and orderby

I am trying to query posts using custom meta fields (date) and order them by the custom meta field (start_date).
I need to look for posts that start on or after todays date or end after today's date.
It all seems to work apart from the order by start date and OR statement, any help appreciated.
<?php
$today = date('y-m-d');
$args = array(
'numberposts' => -1,
'meta_key' => 'start_date',
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'start_date',
'value' => $today,
'compare' => '>='
),
array(
'key' => 'end_date',
'value' => $today,
'compare' => '>'
)
)
);
query_posts( $args );
?>
I am assuming you are MySQL database for wordpress and date format should be YYYYMMDD.
Update date format: use $today = date('Y-m-d'); in stead of $today = date('y-m-d');
Also type=>date in meta_query like below:
$args = array(
'numberposts' => -1,
'meta_key' => 'start_date',
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'start_date',
'value' => $today,
'type' => 'date',
'compare' => '>='
),
array(
'key' => 'end_date',
'value' => $today,
'type' => 'date',
'compare' => '>'
)
)
);
Hope will help!

Resources