Set new url from custom post field

It sounds like the custom_url_posts_event function you’ve written is not being called by WordPress when generating the permalink for a post. This may be because the add_filter call is not properly hooked into WordPress or because the custom_url_posts_event function is not registered correctly with the post_type_link filter.

To fix this, you should first ensure that your add_filter call is properly hooked into WordPress. This can be done by ensuring that the add_filter call is placed inside a function hooked into the init action. Here is an example of how you can do this:

function wpdocs_custom_url_posts_event() {
    add_filter('post_type_link', 'custom_url_posts_event', 11, 4);
}
add_action( 'init', 'wpdocs_custom_url_posts_event' );

In this example, the wpdocs_custom_url_posts_event function is hooked into the init action, which means that it will be called when WordPress is initializing. Inside this function, the custom_url_posts_event function is registered with the post_type_link filter using the add_filter function.

You should also make sure that the custom_url_posts_event function is registered with the post_type_link filter using the correct number of arguments. In your code, the custom_url_posts_event function is defined with two arguments, but the add_filter call specifies that it should have four arguments. This is likely causing an error.

To fix this, you can update the custom_url_posts_event function to accept four arguments, like this:

function custom_url_posts_event($urlsub, $post, $leavename, $sample) {
    // your code here
}

Once you’ve made these changes, your custom_url_posts_event function should be called by WordPress when generating the permalink for a post, and you should be able to update the permalink with the slug of the corresponding event.