Support » Developing with WordPress » custom post type without category in slug

  • Resolved newbee_sue

    (@newbee_sue)


    hi
    I am using wck custom post type with custom post type permalinks so that I can have a custom post type for my articles.

    Until a few months ago the code below worked for removing the rewrite slug “article” from the links to my article pages. I don’t want the category in the link
    Recently my system broke (appears to be a conflict with elementor that I am using on other pages. Elementor is not on article pages) and now while the links work – I can not add a target link to the code and when I am logged in I get a 404 error.

    Would anyone be able to help me work the code so that I don’t need to have the slug in the custom post type. I know that is not the way it usually works but my workaround was successful until now and perhaps someone has another work around.
    thanks

    /**
    functions for removing article from the url
     */
    function na_remove_slug( $post_link, $post, $leavename ) {
    
        if ( 'events' != $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 );
    
    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', 'article', 'source', 'page' ) );
        }
    }
    add_action( 'pre_get_posts', 'na_parse_request' );

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    Your approach should still be valid AFAIK, provided there are no redundant post name slugs among the various post types. Try using the Query Monitor plugin to examine the SQL used to get the requested post/article. It’s failing to find the correct post for some reason, and the reason should be evident in the SQL. In the monitor’s data box, list the queries filtered by caller Main Query.

    Thread Starter newbee_sue

    (@newbee_sue)

    thank you that was a great idea. So it seems that the troublemaker as the plugin says is
    Elementor which I do not use on this page. And which has 2 calls
    Elementor\Core\Base\BackgroundProcess\WP_Background_Process->is_queue_empty()

    Elementor\Core\Base\DB_Upgrades_Manager->start_run()
    1 call
    Elementor\Core\Base\BackgroundProcess\WP_Background_Process->is_queue_empty()
    1 call

    How can I remove that from running on the page. I use plugin organizer to say don’t run elementor on the page but that doesn’t seem to be working. Is there code that I can add as above to block elementor from running on my article post type pages?
    thank you for your help

    Moderator bcworkz

    (@bcworkz)

    I wouldn’t know how to do that, short of deactivating/reactivating the plugin. Way overkill 🙂 You might try asking in Elementor’s dedicated support channel.

    While those calls may be unanticipated, unless they are affecting the main query, they shouldn’t be the cause of the 404 response. When the main query fails to return post data, WP responds with a 404. There’s something about the main query that’s preventing post data from being found. One thing that sometimes happens is something like WHERE 1=0 in the SQL. That will always fail. It’s an indication that WP was unable to properly parse the request. There could be all sorts of other causes, but it’s one thing to watch out for.

    Thread Starter newbee_sue

    (@newbee_sue)

    thank you. You set us on the path of finding the solution to this issue – which was super simple – here is what we realized:

    After doing some more troubleshooting we have found that the 404 is being returned because when Elementor is loaded the post_type is returned as an array with the following:
    Array
    
    (
        [0] => post
        [1] => page
        [2] => e-landing-page
    )
    
    However, when Elementor is disabled the post_type is returned as an array with the following:
    Array
    
    (
        [0] => post
        [1] => article
        [2] => page
    )
    
    Our page uses the custom post type article. When article is in the array the page is found correctly (which is the behavior with Elementor disabled). However, with Elementor enabled this post type is not included in the array resulting in the 404.
    
    We have tried adding hooks to update the post_type however we have not been successful in getting Elementor to include article in the post_type.

    Solution: Elementor>settings> Experiments and set landing page as inactive and this solved the issue

    thank you again

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘custom post type without category in slug’ is closed to new replies.