Home › Forums › Front-end Issues › Custom Post Type: Order posts by a group's field at archive page
Hello guys,
I am trying to fetch all custom posts : missions
and sort them by a date picker field named: mission_date_start
I have created with ACF a fields group for my custom post type posts -> I created among others a group field named: mission_date that has two childs:
mission_date_start: type: date picker – return format ‘Ymd’ – key: field_5b9be275c6962
mission_date_end : type: date picker – return format ‘Ymd’
I created my archive page and has these arguments for my WP_Query
$today = date(‘Ymd’);
$args = array (
‘post_type’ => ‘missions’,
‘orderby’ => ‘meta_value’,
‘order’ => ‘ASC’,
‘post_status’ => ‘publish’,
‘posts_per_page’ => -1,
‘meta_key’ => ‘mission_date_start’,
‘meta_query’ => array(
array(
‘key’ => ‘mission_date_start’,
‘value’ => $today,
‘type’ => ‘DATE’,
‘compare’ => ‘>=’
)
)
);
—————————————————————————–
This is what is suggested from various places this question is asked. It doesn’t work.
I tried to change the values of ‘meta_key’ and ‘key’ to the fields key instead of name (field_5b9be275c6962) and doesn’t work.
I suspect that might be a problem that the field is inside a group field?? Don’t know…
Can someone help me?
This should work with something like:
$args = array(
'post_type' => 'your_custom_post_type',
'meta_key'=> 'your_field_date',
'orderby' => 'meta_value',
'order' => 'ASC'
);
If you’re are working with the archive page, maybe a best way to do it is modify the main query with the pre_get_posts
hook.
For Example:
// in your functions.php
function sort_by_date_my_cpt( $query ) {
if ( is_post_type_archive( 'your_custom_post_type') ) {
$query->set('meta_key', 'your_date_custom_field');
$query->set('orderby', 'meta_value');
$query->set('order', 'ASC');
return;
}
}
add_filter( 'pre_get_posts', 'sort_by_date_my_cpt', 1);
So then, you will to use your WP Loop by default in your archive template.
// in your archive-your_custom_posttype.php or archive.php
<?php if ( have_posts() ) :
<?php while ( have_posts() ) : the_post(); ?>
<?php endwhile; ?>
<?php endif; ?>
dgaitan thank you for your reply.
I tried both ways suggested!
The pre_get_posts hook solutions looks so clean … though either did the trick!
When I placed the arguments suggested at the first solution no custom posts are fetched on my archive page.
When I edit the pre_get_posts hook it even erased all the text from the rest of the page (header and footer).
The only way it fetches results is when I remove from my args the meta_key and order_by options. Using this way though all custom posts are ordered by the date a custom post is published.
I find it really weird.
You must be logged in to reply to this topic.
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.