Skip to main content
Put more concrete sample.
Source Link
hamdirizal
  • 483
  • 2
  • 7

You can filter the slug creation on wp_unique_post_slug hook.

//Register the filter
add_filter('wp_unique_post_slug','prefix_the_slug',10,6);

function prefix_the_slug($slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug){
    //Slug prefix, youGet canvalue putfrom Carbonthe resultscustom here.field
    $prefix = 'my-awesome-prefix';get_post_meta($post_ID,'cw_event_start_date',true);

    //Adjust thisOnly toprefix yourcertain post type and if prefix field existed
    if($post_type==='post' && $prefix && !empty($prefix)){

        //Prefix only if it is not already prefixed
        preg_match ('/^\d\d\d\d\d\d/', $slug, $matches, PREG_OFFSET_CAPTURE);
        if(empty($matches)){
            return $prefix.'-'.$slug;                       
        }
    }
    return $slug;
}

You can filter the slug creation on wp_unique_post_slug hook.

//Register the filter
add_filter('wp_unique_post_slug','prefix_the_slug',10,6);

function prefix_the_slug($slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug){
    //Slug prefix, you can put Carbon results here.
    $prefix = 'my-awesome-prefix';

    //Adjust this to your post type
    if($post_type==='post'){
        return $prefix.$slug;       
    }
    return $slug;
}

You can filter the slug creation on wp_unique_post_slug hook.

//Register the filter
add_filter('wp_unique_post_slug','prefix_the_slug',10,6);

function prefix_the_slug($slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug){
    //Get value from the custom field
    $prefix = get_post_meta($post_ID,'cw_event_start_date',true);

    //Only prefix certain post type and if prefix field existed
    if($post_type==='post' && $prefix && !empty($prefix)){

        //Prefix only if it is not already prefixed
        preg_match ('/^\d\d\d\d\d\d/', $slug, $matches, PREG_OFFSET_CAPTURE);
        if(empty($matches)){
            return $prefix.'-'.$slug;                       
        }
    }
    return $slug;
}
Source Link
hamdirizal
  • 483
  • 2
  • 7

You can filter the slug creation on wp_unique_post_slug hook.

//Register the filter
add_filter('wp_unique_post_slug','prefix_the_slug',10,6);

function prefix_the_slug($slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug){
    //Slug prefix, you can put Carbon results here.
    $prefix = 'my-awesome-prefix';

    //Adjust this to your post type
    if($post_type==='post'){
        return $prefix.$slug;       
    }
    return $slug;
}