(query_post) Display posts based on ACF True/False, ACF Date Range
-
Hi there,
I’ using Advanced Custom Fields to display a variety of fields in a Custom Post Type called “Books”.
I have successful set up the Post Type and it’s unique page template, as well as a query that display posts that are featured in a div at the top, and posts that are not featured underneath.
The query for this is working fine, and was pretty easy for me to sort out. However, I also have two date fields in ACF:
Offer Start Date
Offer End DateI require these fields to be used to help filter the posts that are displayed using todays date.
Any posts that are not within the date range will not be shown.
My dates in ACF are saved as dd/mm/yy and I have tried a number of solutions but can’t seem to work it out. It’s mainly as I have the “featured” meta_key, so I haven’t quite figured out how to set a relationship on the date keys, and not on the featured and have them in the same array (if that makes sense).
Here is my code for the featured books:
$args = array( 'post_type' => 'books', 'showposts' => 2, 'orderby' => 'rand', 'meta_query' => array( array( 'key' => 'featured', 'value' => 1, 'compare' => 'LIKE' ) ) ); $loop = new WP_Query( $args ); while ( $loop->have_posts()) : $loop->the_post();
Here is my code for the non featured books:
$args = array( 'post_type' => 'books', 'posts_per_page' => 40, 'showposts' => 40, 'orderby' => 'rand', 'meta_query' => array( array( 'key' => 'featured', 'value' => 0, 'compare' => 'LIKE' ) ) ); $loop = new WP_Query( $args ); while ( $loop->have_posts()) : $loop->the_post();
My ACF field names for the two date fields are:
Offer Start Date – offer_start_date
Offer End Date – offer_end_dateAny assistance on this would be welcomed. I think I know I need to add a relationship(?) into the meta_query array? Just can’t figure it out, so any pointers?
Thanks in advance!
- The topic ‘(query_post) Display posts based on ACF True/False, ACF Date Range’ is closed to new replies.