Showing post according to post dates - wordpress

i have one event plug-in and now i want to show my event post like current events,upcoming events then past events.i have created one custom post for this and create 2 meta-boxes for start date and end date. But now how i call?

Without more details, I just can say you'll probably need to perform a query similar to this one:
$args = array(
'post_type' => 'events',
'posts_per_page' => -1,
'order' => 'ASC',
'orderby' => 'meta_value_num',
'meta_key' => 'end_date',
'meta_query' => array(
array(
'key' => 'endt_date',
'value' => time(),
'type' => 'numeric',
'compare' => '<'
)
)
);
$past_events = new WP_Query( $args );
This would return the past events, assuming your post type is called events, you have a custom field end_date that stores a numeric timestamp...
You should modify the meta_query accordingly for current and past events, using different value and compare arguments.
Take a look at docs for WP_Query class and in particular to the Custom Field Parameters section.

Related

Wordpress - Ordering custom post type by datepicker repeater values

I have a custom post type called Events. I have a acf field in the events cpt called "Dates" which is a repeater that contains a datepicker field called "Date".
The user can add numerous dates using the datepicker repeater to this event. for example
14/07/2022
18/08/2022
22/01/2023
Its safe to assume the dates will be in the correct order, with the most recent date last.
I'm trying to write a meta query that loops through the events post type and returns the posts in order by the last date in the datepicker repeater
Below doesnt work. any ideas?
$event_args = array(
'post_type' => 'events',
'meta_query' => array(
array(
'key' => 'date_repeater',
'value' => date("Ymd"),
'compare' => '>=',
'type' => 'DATE'
)
),
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_key' => 'date_repeater',
);
$event_posts = new WP_Query( $event_args );

WordPress WP_Query query event post type for events after current time

I don't know why I can't get this to work. So I want to get a list of events that are in the future. The event date is stored as a timestamp. I want to order them by the next event first and the next one after that etc.
Here's what I'm using but it's not ordering by the timestamp and it's even showing events that have passed.
$args = array (
'post_type' => 'race',
'posts_per_page' => -1,
'meta_key' => 'race_date',
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_compare' => '>=',
'meta_value' => time()
);
So for example there's an event with the timestamp 1540322400 and the time of testing it was 1552474027 so I don't know why that event is being returned.
Then for example an event with the timestamp 1556447400 is showing before in order of an event with the timestamp 1551813600.
In this case I'm using Advanced Custom Fields date and time picker and storing it using a custom value U to store a timestamp.
I'm not sure why I can't get this to work.
I think you need to overtly specify a date object comparison for your meta query. Otherwise, your comparison may be comparing strings rather than dates.
You can set a 'type' of DATETIME on the meta query comparison.
Try this query:
$time = date('U'); // same as ACF format
$args = array (
'post_type' => 'race',
'posts_per_page' => -1,
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'race_date',
'value' => $time,
'compare' => '>=',
'type' => 'DATETIME' // Try with DATE if necessary
)
),
);
References
ACF Manual, date time picker
WP Codex, custom field parameters

Wordpress get posts[relationship] from users

I have a case in which I have 2 custom post types, let's say they are projects and teams. Using Advanced custom fields, I have a relationship field while creating a project and I'm assigning which teams are working on it. (Important: there is no custom field in the teams post type). Later on, I want on single-team, to list all the projects that this team was working on.
The example that Advanced custom fields has is where you have a custom field in the team, not in the project, and I need to do the opposite. (Here is how acf documentation is https://www.advancedcustomfields.com/resources/querying-relationship-fields/).
I tried doing this, but it doesn't work, it says that I don't post a correct data.
$team_id = get_the_ID();
$posts = get_posts(array(
'post_type' => 'projects',
'orderby' => 'teams',
'post__in' => $team_id,
));
Since the ACF relationship field is located in your Project custom post type in your single-team.php file where you want to list of the projects on which the team member worked on you can do the following:
$args = array(
'post_type' => 'PROJECTS CPT NAME HERE',
'post_status' => 'publish',
'posts_per_page' => 3,
'orderby' => 'date',
'order' => 'DESC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'ACF RELATIONSHIP FIELD NAME HERE',
'value' => get_the_ID(), // the ID of the member
'compare' => 'LIKE',
)
)
);

WordPress: one condition for multiple values but one key when querying with custom fields

I am working on some kind of booking extension for a custom post type in Wordpress. The admin can choose the dates when the object is booked. The booked date is saved as a new custom field with the key "booking" and the date in the format "yymmdd". So one object can have multiple custom fields with the same key, but multiple values.
Now I need to query the objects that are available for a specific date. The following is one of my last attempts.
$args = array(
'post_type' => 'object',
'meta_query' => array(
array(
'key' => 'booking',
'value' => array('131020', '131021', '131022'),
'compare' => 'NOT IN'
)
)
);
$query = new WP_Query( $args );
From my logic, I am searching for an object that is available from 131020 to 131022. What WordPress sees to do though is comparing each of the available multiple values individually. So if the element is booked on 131019 and on 131020, WordPress still returns it as available, because 131019 is not in the values above.
What I need is a logic that say "if none of the multiple values is among the mentioned numbers, return the post"
I also tried using just a serialized array for all of the dates, but this didn’t work either. I am sure there might be a simple trick, but after doing a lot of research I am stuck here.
Any ideas?
Thanks
After many other approaches, I finally ended up with the following "solution".
I saved all the dates as an array and was using NOT LIKE for each of the dates that needed to be available. The code from above would look like this:
$args = array(
'post_type' => 'object',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'booking',
'value' => '131020',
'compare' => 'NOT LIKE'
),
array(
'key' => 'booking',
'value' => '131021',
'compare' => 'NOT IN'
),
array(
'key' => 'booking',
'value' => '131022',
'compare' => 'NOT IN'
)
)
);
$query = new WP_Query( $args );
Note relation => 'AND' at the beginning. As I understand, this is the default, but in case you use 'OR' here this wouldn’t work either.
Still happy about any comment on both of the approaches.

Posts query including meta and greater than date

I'm struggling to get a working solution with this wp_query. I currently have some custom settings which are assigned to posts, one is whether or not the post is 'featured' and the second is a date and time for the post to end (no longer display in the results). I have the query working with the feature, but just need to work this end date into it, here is the query working find with the 'featured':
WP_Query('meta_key=skyali_feature&showposts=4&orderby=post_date');
The end date is set in the wp_postmeta table where meta_key is 'the_date' and the meta_values look like this '05/16/2013 05:24'. I would like to edit the above query where if 'the_date' has been set posts are only included if the 'the_date' is greater that todays date and time.
Here is my failed attempt:
WP_Query(
'meta_key=skyali_feature&meta_key=the_date&meta_compare=>=&meta_value='
.date('d/m/Y H:i')
.'&showposts=4&orderby=post_date&orderby=the_date'
);
I had to do something very similar recently and ended up needing to use the meta_query property instead. You'll want to do something like this:
$today = date('Ymd');
$args = array(
'post_type' => 'post',
'posts_per_page' => '4',
'meta_key' => 'the_date',
'meta_query' => array(
array(
'key' => 'skyali_feature'
),
array(
'key' => 'the_date',
'value' => $today,
'compare' => '>='
)
),
'orderby' => 'meta_value_num',
'order' => 'ASC'
);
$your_custom_query = new WP_Query($args);
A few notes...
I only needed to filter by date in my example, but it looks like you'll need to do date/time in yours. (You can just adjust the first line for the $today variable using the format you wish).
Use posts_per_page instead of showposts. showposts is deprecated.
Notice that I have included the meta_key twice (once at the top level of the array and once as an element in the meta_query array. There's a known bug where you can't sort your results by the key if you don't include it this way. I fought that one for a while too!
Hope this helps, have fun!
[edit] Forgot to add your skyali_feature key back into the array.
for people using the advanced custom field plugin with a date data type, this is what you need for dates greater or equal than today:
$today = date('Ymd');
$args = array(
'post_type' => 'post',
'meta_key' => 'end-date',
'meta_query' => array(
array(
'key' => 'end-date'
),
array(
'key' => 'end-date',
'value' => $today,
'compare' => '>='
)
),
'orderby' => 'meta_value',
'order' => 'ASC'
);
$your_custom_query = new WP_Query($args);
for people using Custom metadata manager you'll find that a datepicker field is stored as timestamp.
So in a similar case the above example isn't working and you may have php sort out the value you need to compare against. And the timestamp for a day earlier at 23:59:59 it'll do the job:
$yesterday = strtotime('yesterday 23:59:59');
$args = array(
'post_type' => 'post',
'meta_key' => 'end-date',
'meta_query' => array(
array(
'key' => 'end-date'
),
array(
'key' => 'end-date',
'value' => $yesterday,
'compare' => '>='
)
),
'orderby' => 'meta_value',
'order' => 'ASC'
);
$your_custom_query = new WP_Query($args);
If you also want to take account of the timezone setting for the blog use current_time() as in the following example:
$now = current_time('timestamp');
$yesterday = mktime(23, 59, 59, date('n',$now), date('j',$now)-1);

Resources