Support

Account

Home Forums ACF PRO Add a custom field to post permalink

Solved

Add a custom field to post permalink

  • Hello,
    I would like to add the value of a custom field to the permalink of the posts.
    Many posts will have the same title (job offers) and I would like to avoid the numbers in the URLs (job, job-2, job-3…)
    So the idea is to append the reference number of the post to the permalink: job-388, job-401…
    I’ve tried the WP filter post_type_link but I end up with 404, even after flushing the rewrite rules. And it doesn’t solve the numbering issue because the permalinks created are like: job-388, job-2-401…
    Here is my code:

    
    function fs_filter_offers_permalink( $url, $post ) {
    
        $ref = get_field( 'ref', $post->ID );
        $link = $post->post_name;
        
        if ( $ref && 'offer' === get_post_type( $post->ID ) ) {
            $url = esc_url( home_url('offer') ) ).'/'.$link.'-'.$ref.'/';
        }
    
        return $url;
    }
    add_filter( 'post_type_link', 'fs_filter_offers_permalink', 10, 2 );
    

    Does anyone have some hints?

  • You need to change the post slug.

    
    add_action('acf/save_post', 'modify_post_slug');
    function modify_post_slug($post_id) {
      $ref = get_field('ref', $post_id);
      if ($ref) {
        $post = get_post($post_id);
        $slug = sanitize_title($post->post_title.' '.$ref);
        $post->post_name = $slug;
        remove_filter('acf/save_post', 'modify_post_slug');
        wp_update_post($post);
        add_action('acf/save_post', 'modify_post_slug');
      }
    }
    
  • Thanks a lot!!
    It’s perfectlty working 🙂

  • @hube2

    Hello John,
    is it correct that I need to save the Post twice to the the SLUG changed?
    Or am I doing something wrong?

    function modify_post_slug($post_id) {
      $artist = get_field('artist', $post_id);
      $album = get_field('album', $post_id);
      $datum = get_field('datum', $post_id);
    	$ref = $artist.' '.$album.' '.$datum;
      if ($ref) {
        $post = get_post($post_id);
        $slug = sanitize_title($ref);
        $post->post_name = $slug;
        remove_filter('acf/save_post', 'modify_post_slug');
        wp_update_post($post);
        add_action('acf/save_post', 'modify_post_slug');
      }
    }
    add_action('acf/save_post', 'modify_post_slug');

    Cheers,
    Denis

  • @deniscgn it should be working, but you can try increasing the priority of your action

    
    add_action('acf/save_post', 'modify_post_slug', 20);
    
  • @hube2

    Hi John, that works fine.
    But, is there a way for BULK changing? I got round 20000 posts, that will be a big work, open and re-save them.
    Any Suggestion?
    Cheers,
    Denis

  • There isn’t any real way to do this in bulk with 20k posts.

    The biggest issue is the number of posts.

    You could possibly build an action, I don’t have exact code.

    
    add_action('init', 'my_modify_slugs');
    function my_modify_slugs() {
      // query posts
      // limit posts to 100
      // use the "paged" arg of WP_Query
      // loop over posts and alter each post
    }
    

    Basically you load the page, the action gets 100 posts and alters them. Then you update the “paged” argument to get the next page of posts. You step through the posts 100 at a time. You might be able to get away with doing more than 100 at a time. You could increase this number and test until your site times out and then lower it to a number that can be done before timeout. Then step through pages.

  • Hi !
    I needed exactly the same thing, thanks a lot 🙂
    I have a small question about that, it works well when mannualy updating or manually creating a new post but it seems that ‘acf/save_post’ is never fired when posts are added from API.

    Do you have an idea to fire the function even when new content is added from api maybe?

    Regards

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

You must be logged in to reply to this topic.