Question

Custom code, removing CPT from URL, no longer working

  • 13 October 2021
  • 0 replies
  • 2 views

Badge

HI all

 

I tried Elementor support but as its custom code, they wont help. Ill just paste what i sent them below, and hopefully someone here will know whats going on and point me in the right direction :)

 

Quite often, i create a custom post type but i add a custom code snippet to remove the title from the URL. So for example:

www.domain.com/custom-post-type/Service-title
becomes
www.domain.com/service-title

Ive been doing this for years. The same way, via the same custom snippets of php code. I found the code on a popular site with plenty of others also using the code successfully for years. I will post the PHP snippet I use below.

 

Anyway... Elementor doesn't like it anymore. I have narrowed it down to something in the 3.3.0 update.... because it works fine in 3.2.5 version. Anything 3.3.0 and beyond, makes the custom post go to a "page not found" screen. (FYI: this is the Elementor standard versions, NOT pro)

Any idea why this is, and can you fix it as I have so many sites running this same technique.

Here is a staging site fully updated, and not working: https://pension-refunds.co.uk/stage/copia-wealth-management/
Live site here working but with version 3.2.5: https://pension-refunds.co.uk/copia-wealth-management/

 

function na_remove_slug( $post_link, $post, $leavename ) {

if ( 'custom-landing-pages' != $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', 'na_remove_slug', 10, 3 );
?>

<?php

function na_parse_request( $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', 'custom-landing-pages', 'page' ) );
}
}
add_action( 'pre_get_posts', 'na_parse_request' );

 


0 replies

Be the first to reply!

Reply