Support

Account

Home Forums ACF PRO Post-Slug from diffrent fields

Solving

Post-Slug from diffrent fields

  • Hello,

    can some tell me, how I can create the POST SLUG from diffrent cunstom fields?
    Most of my users are not familiar creating SEO friendly POST-SLUGS.
    So I let them fill out some custom fields.

    Now I like to create the Slug from this fields.

    How does this work? Is there a hook I can use during create or save post?

    Thanks and Cheers,
    Denis

  • Yes you can. You need to create an acf/save_post filter http://www.advancedcustomfields.com/resources/acfsave_post/

    In the filter get the values from the field and then use wp_update_post() to set the post values you want to change https://codex.wordpress.org/Function_Reference/wp_update_post

  • HI John,

    so I need 2 functions?

    <?php
    
    function my_acf_save_post( $post_id ) {
        
        // bail early if no ACF data
        if( empty($_POST['acf']) ) {
            
            return;
            
        }
      
        // specific field value
        $my-new-slug = $_POST['acf']['my-slug-part1'].'-'.$_POST['acf']['my-slug-part2'].'-'.$_POST['acf']['my-slug-part3'];
        
    }
    
    // run before ACF saves the $_POST['acf'] data
    add_action('acf/save_post', 'my_acf_save_post', 1);
    
    ?>

    How will I save the $my-new-slug to the post-slug?
    Is there also a filter that makes the slug like it will be done by wordpress. deleting unwanted charachters?

    Cheers,
    Denis

  • 
    <?php
    
    function my_acf_save_post( $post_id ) {
        // bail early if no ACF data
        if( empty($_POST['acf']) ) {
            return;
        }
        // specific field value
        $my_new_slug = $_POST['acf']['my-slug-part1'].'-'.$_POST['acf']['my-slug-part2'].'-'.$_POST['acf']['my-slug-part3'];
        $post = array(
          'ID' => $post_id,
          'post_name' => $my_new_slug
        );
        wp_update_post($post);
    }
    
    // run before ACF saves the $_POST['acf'] data
    add_action('acf/save_post', 'my_acf_save_post', 1);
    
    ?>
    
  • Hey John,

    when I use the code

    /* ACF Custom Slug */
    
    function my_acf_save_post( $post_id ) {
        // bail early if no ACF data
        if( empty($_POST['acf']) ) {
            return;
        }
        // specific field value
        $my_new_slug = $_POST['acf']['field_5705116adbe84'].'-'.$_POST['acf']['field_5705117fdbe85'].'-'.$_POST['acf']['field_5705118edbe86'];
        $post = array(
          'ID' => $post_id,
          'post_name' => $my_new_slug
        );
        wp_update_post($post);
    }
    
    // run before ACF saves the $_POST['acf'] data
    add_action('acf/save_post', 'my_acf_save_post', 1);

    I get an

    ERROR 500 – Internal Server Error

    When I used it the first time it worked…but now I get the Error 500.

    Cheers,
    Denis

  • There is a PHP error, 500 internal server error, you must be using an MS server.

    Turn on error reporting so that you can see the real error

    
    // add to config.php
    define('WP_DEBUG', true );
    define('WP_DEBUG_DISPLAY', true);
    
  • This reply has been marked as private.
  • Seriously, I dislike MS servers. That error message is useless, like all MS errors. I’m not familiar with your server setup so I can’t really advise you on how to get it to display the real PHP errors that will give you useful information. I’d start by contacting the host and asking them.

    As far as the access goes, debugging other peoples site’s or doing dev work on them is not something I do here. This is a user forum, users helping users.

    As far as insert post, yes, since you’re running you function before ACF does you may need to insert the post first, when inserting the post there are some other requirements https://developer.wordpress.org/reference/functions/wp_insert_post/, like inserting content because it’s required.

  • This reply has been marked as private.
  • At least you’re getting a real error now, but I don’t know what it means. The code you’ve posted here should not be causing that error so you’ve probably got a something wrong somewhere else.

  • @hube2

    Hey John,

    I will use this solution.

    Thanks a lot,
    Denis

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

You must be logged in to reply to this topic.