Support » Plugin: Beautiful taxonomy filters » Custom post type gets duplicated post type name in URL on filtering

  • 99w

    (@ninetyninew)


    Hi – great plugin.

    We’ve been using your plugin for a few years on an old website we are currently upgrading, we are working on a new installation of WordPress for it. We have a custom post type ‘horse’ the URLs are rewritten to ‘horses’.

    When you filter from domain.com/horses the filtering works as expected but the URL has a duplicated horses included e.g. domain.com/horses/horses/locations/spain/ is there a way to fix this, it doesn’t occur on the old website? Maybe I added some extra code to the old website to get this to work originally which I have since forgotten about.

    Thanks,

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Jonathandejong

    (@jonathandejong)

    Hi @ninetyninew

    Hmmm no that just sounds incorrect. Do you have a taxonomy with the same slug as the CPT?

    This is essentially what builds the URL:

    
    $new_url = trailingslashit( get_post_type_archive_link( $current_post_type ) );
    
    //Get the taxonomies of the current post type
    $current_taxonomies = btf_get_current_taxonomies( $current_post_type );
    if ( $current_taxonomies ) {
    	foreach ( $current_taxonomies as $key => $value ) {
    
    		//check for each taxonomy as a $_POST variable.
    		//If it exists we want to append it along with the value (term) it has.
    		$term = ( isset( $_POST[ 'select-' . $key ] ) ? $_POST[ 'select-' . $key ] : false );
    		if ( $term ) {
    			$term_object = get_term( $term, $key );
    			//If the taxonomy has a rewrite slug we need to use that instead!
    			if ( is_array( $value->rewrite ) && array_key_exists( 'slug', $value->rewrite ) ) {
    				$new_url .= trailingslashit( $value->rewrite['slug'] . '/' . $term_object->slug );
    			} else {
    				$new_url .= trailingslashit( $key . '/' . $term_object->slug );
    			}
    		}
    	}
    }
    

    As you can see it just grabs the cpt archive link and then starta appending the taxonomy -> term values to the url (Yeah I really should rewrite this to use something like http_build_query instead).

    Thread Starter 99w

    (@ninetyninew)

    Thanks for the info.

    I am wondering if this is because I am running the website currently on http://localhost/folder-name maybe?

    Thread Starter 99w

    (@ninetyninew)

    Hey @jonathandejong,

    So looking into this, for any website which has a custom post type defined and if it uses the rewrite slug option when registering the post type then your code above would set the $new_url variable as https://domain.com/horses on the first line.

    Then your code later on would concatenate the rewrite slug again causing the duplication in this line:

    $new_url .= trailingslashit( $value->rewrite['slug'] . '/' . $term_object->slug );

    Is this something you can look into fixing? If I get time I can look into a fix for you but I am not familiar with all the other code/functionality in this plugin which an edit here could effect?

    Thanks,

    • This reply was modified 5 months ago by 99w.
    • This reply was modified 5 months ago by 99w.
    Plugin Author Jonathandejong

    (@jonathandejong)

    Hi @ninetyninew

    hmm ah I see what you mean!
    Looks like I’ve made a booboo.

    If you like to do a PR there’s a github repo and you’re more than welcome to it.
    I don’t think it’s going to cause issues fixing it here some other place.

    I’ll try to schedule in to do a fix myself but it’s hard to find the time right now 🙂
    Going on parental leave in a few weeks and hopefully I’ll have a bit more time then.

    Thread Starter 99w

    (@ninetyninew)

    Hey @jonathandejong,

    I’ve looked into this a bit more, so it appears the issue arises because (for rewrite based CPTs) this line:

    $new_url = trailingslashit( get_post_type_archive_link( $current_post_type ) );

    Will return domain.com/horses

    Then later on it gets duplicated in this line:

    $new_url .= trailingslashit( $value->rewrite['slug'] . '/' . $term_object->slug );

    This line appears to be okay, it’s just that it already includes horses but the value of $new_url already has it and we are concatenating.

    I changed this to:

    $new_url = get_site_url();
    $new_url .= trailingslashit( '/' . $value->rewrite['slug'] . '/' . $term_object->slug );

    Which rebuilds the entire $new_url variable from scratch. It appears to work fine. However resetting the $new_url variable here isn’t the most elegant solution, you may want to consider some way of getting that $new_url to be set properly for rewrite based CPTs before it hits this condition.

    It’s worth noting I didn’t check any other functionality other than rewritten CPTs after doing this unsure if it may have effected anything else.

    Hopefully that helps in finding a solution for this issue when you come to update the plugin. If you need any further info just let me know and if you could keep me posted on this thread when you’ve checked it out as i’d like to push any updates on this when available. For now i am going to leave this plugin edit on my site, hopefully I can then just upgrade the plugin with the fixes later if in the next release.

    Sorry I didn’t have time right now to setup the repo on GitHub, raise a PR, etc – although as above I don’t think this code is quite the perfect solution to the issue just yet.

    Thanks

    • This reply was modified 4 months, 1 week ago by 99w.
    • This reply was modified 4 months, 1 week ago by 99w.
    Thread Starter 99w

    (@ninetyninew)

    Just a note on the comment above. I tested this further and it works but only for 1 filter, if I use more than 1 filter it doesn’t.

    Therefore for my scenario I removed what I had completely then below this line:

    $new_url = $this->append_get_parameters( $new_url );

    I added:

    $new_url = str_replace( '/horses/', '/', $new_url );

    Which gave me the correct URL when using the filters but the page could not be found, so I had to amend the generate_rewrite_rules() function with a change to a line in the taxonomy loop to this:

    $rewrite_slug = ( ! empty( $taxonomy->rewrite['slug'] ) ) ? str_replace( 'horses/', '', $taxonomy->rewrite['slug'] ) : $query_var;

    Which works after flushing permalinks.

    If anyone is reading this looking to apply a similar temporary fix you should just be able to change the references to horses with your rewritten post type slug.

    • This reply was modified 4 months ago by 99w.
Viewing 6 replies - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.