0

I am trying to modify the link structure of Wordpress, I want to include certain variables, but first I am trying a simple thing like changing the slug /oppositions/ to /test/ I have the following code:

function na_remove_slug( $post_link, $post, $leavename ) {
 
        if ( 'oposiciones_udeka' != $post->post_type || 'publish' != $post->post_status ) {
 
        return $post_link;
 
    }
 
        $post_link = str_replace( '/oposiciones/', '/test/', $post_link );
 
        return $post_link;
 
}
 
add_filter( 'post_type_link', 'na_remove_slug', 10, 3 );
 
function change_slug_struct( $query ) {
 
        if ( ! $query->is_main_query() || 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
 
        return;
 
    }
 
                if ( ! empty( $query->query['name'] ) ) {
 
                $query->set( 'post_type', array( 'post', 'oposiciones_udeka', 'page' ) );
 
                } elseif ( ! empty( $query->query['pagename'] ) && false === strpos( $query->query['pagename'], '/test/' ) ) {
 
                $query->set( 'post_type', array( 'post', 'oposiciones_udeka', 'page' ) );
 
        // We also need to set the name query var since redirect_guess_404_permalink() relies on it.


    $query->set( 'name', $query->query['pagename'] );

}


}
 
add_action( 'pre_get_posts', 'change_slug_struct', 99 );

The first part works correctly, if I access the WP admin and see the link from the admin panel the link appears modified, but when I access the URL it gives me a 404 error and the link with /oppositions/ continues to work correctly, I understand that the error It is in the second part of the code, which is the one that updates the links, but I do not understand where the error is, I have based myself on the following codes Link 1 Link 2

0

Your Answer

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

Browse other questions tagged or ask your own question.