Home › Forums › ACF PRO › Add a custom field to custom type permalink › Reply To: Add a custom field to custom type permalink
Sorry John for this very long delay.
I used pre_get_posts hook for the archive page of my custom type, and change the post order by ACF field as below:
function legal_annonce_single_query( $query ) {
if( is_admin() ) {
return $query;
}
if(!is_admin() // Doesn't run on admin pages even though I have the above return
&& $query->is_main_query() // Cibler uniquement la requête principale
&& $query->query_vars['post_type'] == 'annoncelegale' // Actionner uniquement pour le post type Annonce légale
)
{
$query->set('meta_key','reference_al'); // ACF field Legal notice reference
$query->set('orderby', 'meta_value_num') // / Sort by the specified custom field
$query->set('order', 'DESC'); // / Descending order
}
}
add_action( 'pre_get_posts', 'legal_annonce_single_query' );
But I can’t see how I can alter the query to return the correct post.
Do you have an idea ?
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 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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.