0

I've create a new custom post type "Event". I've added a custom field (Post Object) to the post type "Post" were you can select the corresponding Event.

So I create post new post and then I select Event X.

I want my URL to be domain.com/news/event-x/post-name.

I've create a function:

function custom_url_posts_event ($urlsub, $post) {
    if ($post->post_type == 'post') {
            
        //get event id
        $eventId = get_post_meta($post->ID, 'event_obj', true);
       
        //get venue slug
        $eventSlug = get_post_field( 'post_name', $eventId );
        
        $eventSlugSlash = '/'.$eventSlug;
        
        if (!empty($eventSlug)) {
            $point = strrchr(rtrim($urlsub, "/"), '/');
            $urlsub = str_replace($point, "/$eventSlug$point", $urlsub);
        }
    }
    return $urlsub;
}

add_filter('post_type_link', 'custom_url_posts_event', 11, 4);

When I output $urlsub I get the correct URL: domain.com/news/event-x/post-name So I guess that the filter above works correctly.

But the actual URL is still domain.com/news/post-name The URL isn't updated with the Event slug (event-x in this case).

I've also tried to change the permalink structure under Settings with: Custom struction: domain.com/news/%event_obj%/%postname%/

But %event_obj% is literally printed in the URL.

1 Answer 1

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.