• Resolved tomzorz

    (@tomzorz)


    I’ve set up a product catalog website with a cpt Public Products which is coupled to a custom taxonomy Public Category. I’ve set up my cpt with a rewrite:

    'rewrite' => array(
        'slug'       => 'prod/%tsrc_public_category%',
        'with_front' => true,
    )

    so my products live under the relevant custom terms. Eg prod/pots/antique-pots/brown-old-pot.
    I’ve used a hook on ‘post_type_link’ to accomplish that:

    add_filter('post_type_link', function($post_link, $id = 0){
    $post = get_post($id);
    if ( is_object( $post ) ){
        $terms = wp_get_object_terms( $post->ID, 'tsrc_public_category' );
        if( $terms ){
            $slug = get_term_parents_list($terms[0], 'tsrc_public_category', ['format'=>'slug','link'=>false]);
    
            return str_replace( '%tsrc_public_category%/' , $slug , $post_link );
        }
    }
    
    return $post_link;
    });

    All works well with this setup, except for the sitemap. It still shows my cpt products with links like prod/%tsrc_public_category%/brown-old-pot/

    Is there any way to fix this? A filter on TSF’s sitemap? An edit to my rewrite method from above?

    I’m aware sitemaps could be overrated, but atm we’re doing a website revamp so re-indexing is of a high importance.

    Any help would be greatly appreciated!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Sybre Waaijer

    (@cybr)

    Hello!

    Is the “Public Category”-category registered together with its “Public Products”-post type at init or at a later action? It seems to me that the wp_get_object_terms() returns a false-esque value.

    Also, that post_type_link filter seems rather a bottleneck; you may want to assert the correct post type before querying all the terms.

    Thread Starter tomzorz

    (@tomzorz)

    Hi, thanks for your response!
    I found the culprit (or at least a fix) and it’s quite a surprising one to me – setting the priority of the post_type_link (to 1 instead of the default 10) fixes the issue.

    Also thanks for me pointing out the bottleneck, I put it a conditional check before running the rewrite!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Sitemap showing rewrite placeholder instead of category for cpt’ is closed to new replies.