Support

Account

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 ?