Home Forums ACF PRO Add a custom field to custom type permalink


Add a custom field to custom type permalink

  • Hello,
    I would like to add the value of a custom field number to the permalink of custom type. The permalinks are create; but I stil have 404 after saving the posts.
    CTP : annoncelegale
    ACF field : reference_al

    Here is my code:

    // Rewrite rule
    function al_cpt_acf_permalink($post_link, $post = 0){
    	if ($post->post_type === 'annoncelegale') { // Sélection du CTP "Annonce Légale"
            $post_id = get_the_ID($post);
            $home_url = get_site_url(); // Url de la home
            $ref_value = get_field('reference_al',$post_id); // Champ ACF Référence de l'annonce légale
    		return $home_url . '/annonces-legales/'.$ref_value; // Génération de la nouvelle url
    	} else {
    		return $post_link;
    // Need to change the post slug.
    add_action('acf/save_post', 'modify_post_slug');
    function modify_post_slug($post_id) {
        if ($post->post_type === 'annoncelegale') { 
        $ref_value = get_field('reference_al',$post_id);
            if ($ref_value) {
                $post = get_post($post_id);
                $slug = sanitize_title($ref_value);
                $post->post_name = $slug;
                remove_filter('acf/save_post', 'modify_post_slug');
                add_action('acf/save_post', 'modify_post_slug');

    What I do wrong ?

  • Hi ACF team,

    Do you have time to answer my problem ?
    Best Regards,

  • In order for your altered permalinks to work you’ll need to add rewrite rules sp that WP can find the correct post. You can’t just modify the link that is output. See

  • 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 :

    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() { 

    How can I attribute ref_acf to the ACF field ?
    <?php the_field( ‘reference_al’ ); ?>

    In advance, thank you for your help.

  • That looks like it will work.

    You will also need to alter the query that is done on the post type using a pre_get_posts action. You need to check for the query var you added and alter the query to return the correct post.

  • Ok John.
    I will try to modify the existing WP_Query tomorrow.

    End of the work day for me. 😉

  • 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 ?

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.