U
U
unionfourman2015-11-11 16:14:40
WordPress
unionfourman, 2015-11-11 16:14:40

How to change custom post type url to avoid 404 errors?

Using the Types plugin, we created a directory (product/%category%/%postname%/)
After that, the product is now unnecessary and the product was cut out, in the "usual" way

function gp_remove_cpt_slug( $post_link, $post, $leavename ) {
    if ( 'product' != $post->post_type || 'publish' != $post->post_status ) {
        return $post_link;
    }
    $post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
    return $post_link;
}
add_filter( 'post_type_link', 'gp_remove_cpt_slug', 10, 3 );
function gp_parse_request_trick( $query ) {
    if ( ! $query->is_main_query() )
        return;
 

    if ( 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
        return;
    }
    if ( ! empty( $query->query['name'] ) ) {
        $query->set( 'post_type', array( 'post', 'page', 'product' ) );
    }
}
add_action( 'pre_get_posts', 'gp_parse_request_trick' );

but the situation is a bit "unusual" there is /%category%/ .
Everything is fine when going to /%category%/, but /%category%/%postname%/ gets 404 error.
How to fix it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
U
unionfourman, 2015-11-16
@unionfourman

In general, the issue was resolved. If I understand correctly, the wp-types plugin has been updated and now it is possible to edit the url.
Also worth the plugin Custom Post Type Permalinks

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question