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 ?
Ok John.
I will try to modify the existing WP_Query tomorrow.
End of the work day for me. 😉
Thank a lot John for your answer,
I’m making slow progress on this rewrite.
but I can’t put the value of my field in my rewrite rule
I want a permalink like this example : https://www.domain.com/annoncelegale/L010422
There is the code :
// Custom URL Rewrite with Parameter on WordPress for Legal Notices with reference
add_filter('query_vars', 'annonce_legale_custom_query_var');
function annonce_legale_custom_query_var($vars) {
$vars[] = 'ref_acf';
return $vars;
}
// Defining the rewrite rule for Legal Announcements
add_action('init', 'annonce_legale_add_rewrite_rule');
function annonce_legale_add_rewrite_rule() {
add_rewrite_rule(
'annonces-legales/([^/]+)/?',
'index.php?post_type=annoncelegale&ref_acf=$matches[1]',
'top'
);
}
How can I attribute ref_acf to the ACF field ?
<?php the_field( ‘reference_al’ ); ?>
In advance, thank you for your help.
Hi ACF team,
Do you have time to answer my problem ?
Best Regards,
Benoît
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.