Home › Forums › General Issues › Hide posts if custom field date is less than current date?
Hello,
I am creating a custom post type archive to display a list of training courses sorted by a custom field “course_start” (date picker – yymmdd) and by the “course_type” which is “2 Day TEFL Fast-Track Course”. The custom post type name is “courses”.
What I need to do is hide any “posts” which have a course_date older than the current date (ie only show courses that are happening today or in the future).
Below is my current WP Query
<?php
$args = array(
'post_type' => 'courses',
'meta_key' => 'course_start',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'posts_per_page' => '4',
'paged' => $paged,
'meta_query' => array(
array(
'key' => 'course_type',
'value' => '2 Day TEFL Fast-Track Course',
'compare' => '='
)
),
);
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query($args);
?>
How would I go about doing this?
Thank you.
Hi @deset
Before I start testing your code, can you please read over these existing similar posts?
http://wordpress.org/support/topic/plugin-advanced-custom-fields-sorting-by-date-picker
Looking at these pages you can see the main issue with your meta query is the compare and value are wrong
Thanks
E
I’ve been having some trouble with this as well. I have a “Upcoming Events” page, and a “Past Events” page. Each event has a custom field called “event_date”.
I want to create a loop that displays all of the events greater than today.
From what I’ve gathered in the three links above, I would put this in my functions.php file:
// CREATE UNIX TIME STAMP FROM DATE PICKER
function custom_unixtimesamp ( $post_id ) {
if ( get_post_type( $post_id ) == 'event_type' ) {
$event_date = get_post_meta($post_id, 'event_date', true);
if($event_date) {
$dateparts = explode('/', $event_date);
$newdate1 = strtotime(date('d.m.Y H:i:s', strtotime($dateparts[1].'/'.$dateparts[0].'/'.$dateparts[2])));
update_post_meta($post_id, 'unixstartdate', $newdate1 );
}
}
}
add_action( 'save_post', 'custom_unixtimesamp', 100, 2);
Then I would add something like this to my page template:
<?php
$today = time();
$args = array(
'post_type' => 'event_type',
'posts_per_page' => 5,
'meta_query' => array(
array(
'key' => 'unixstartdate',
'compare' => '>=',
'value' => $today,
)
),
'meta_key' => 'event_date',
'orderby' => 'meta_value',
'order' => 'ASC',
);
$query = new WP_Query( $args );
$event_type = $query->posts;
?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
Right now that’s not turning up any results. My post-type is called “event_type”, and the key is “event_date”.
Any thoughts on where I’m going wrong?
The topic ‘Hide posts if custom field date is less than current date?’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We’re hard at work on ACF 6.1, and Beta 1 is now available 🚀
— Advanced Custom Fields (@wp_acf) March 16, 2023
This release includes custom post type and taxonomy registration, an improved experience when selecting field types, PHP 8.1 and 8.2 compatibility, and more!
Let’s take a look 🧵https://t.co/Y0WcAT11l4
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.