Permalinks: How to remove "listing-category" and "listing" from permanent links?

HivePress default free plugin template results in a following URL scheme, which i would like to change as shown:

/listing-category/categorynamehere/

/categorynamehere/

/listing/abc-def/

/categorynamehere/abc-def/

I have spent around 1 hour asking AI to give me PHP code/.htaccess rules (Homepage and category tags may continue redirecting to a old link scheme) or using plugin “Redirects”

but failed to find a working solution. Permalink change seems to be also related on re-saving configuration at /wp-admin/options-permalink.php

My categories count would be limited (low number), so maybe i can define these manually if necessary.

Hi,

I hope you had a lovely weekend.

HivePress is built on top of the WordPress database and core concepts, meaning a listing is a custom post type, and a listing category is a custom taxonomy. Because of this, the solution you’re looking for isn’t specific to HivePress itself, so I’d recommend searching for solutions related to custom post types + custom taxonomies in general, since that’s really the underlying structure here.

By default, WordPress uses /taxonomy/term-slug for custom taxonomies and /post-type/post-slug for custom post types. These URL segments are how WordPress determines which taxonomy or post type to look up the entry in, and which layout/template to display.

For reference, in case it helps:

  • Listings use the hp_listing post type
  • Categories use the hp_listing_category taxonomy

I found a couple of links on this topic that might be useful, hope this helps point you in the right direction:

I have spent/wasted around 1 hour with Hive and other AI and unable to make it working. Highest achievement is that listings URLs changes like i wanted

/listing/abc-def/

/categorynamehere/abc-def/

after applying PHP snippet:

// 1. Change displayed URL: /listing/saa/ → /categoryname/saa/
add_filter('post_type_link', function($url, $post) {
    if ('hp_listing' !== $post->post_type) return $url;
    $terms = wp_get_post_terms($post->ID, 'hp_listing_category');
    if ($terms && !is_wp_error($terms)) {
        return home_url('/' . $terms[0]->slug . '/' . $post->post_name . '/');
    }
    return $url;
}, 10, 2);

// 2. Make WordPress understand the new URL pattern (per-category rules)
add_action('init', function() {
    $terms = get_terms(['taxonomy' => 'hp_listing_category', 'hide_empty' => false]);
    if (!is_wp_error($terms)) {
        foreach ($terms as $term) {
            add_rewrite_rule('^' . $term->slug . '/([^/]+)/?$', 'index.php?hp_listing=$matches[1]', 'top');
        }
    }
});

, but in such case, listings pages like /categoryname/saa/ shows “Nothing found” error while Hive AI commenting it " HivePress has its own template and query logic that doesn’t fully respect custom rewrite rules". If anyone knows the fix, let me know.

Unlike above mentioned objective, second objective that I had, was:

/listing-category/categorynamehere/

/categorynamehere/

and that could be partly achieved at /wp-admin/options-permalink.php setting “Category (Listing)” field to for example letter “c” which correctly changes /listing-category/categorynamehere/ to /c/categorynamehere/ and that looks better to me (shorter).

Please note that the issue is not related to HivePress framework – it just registers the post type “hp_listing” and taxonomy “hp_listing_category”. WordPress then takes care of URL routing and permalinks; we only have custom rewrite rules for static pages like “/submit-listing”. I recommend trying the code snippet below; please re-save permalinks in Settings/Permalinks after adding it:

// rewrite /listing/slug to /category/slug
add_filter(
	'post_type_link',
	function ( $permalink, $post ) {

		if ( $post->post_type !== 'hp_listing' ) {
			return $permalink;
		}

		$terms = get_the_terms( $post->ID, 'hp_listing_category' );

		if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
			// Use the first assigned category
			$term = array_shift( $terms );

			return home_url( '/' . $term->slug . '/' . $post->post_name . '/' );
		}

		// Fallback if no category
		return home_url( '/' . $post->post_name . '/' );

	},
	10,
	2
);


add_action(
	'init',
	function () {

		add_rewrite_rule(
			'^([^/]+)/([^/]+)/?$',
			'index.php?hp_listing=$matches[2]',
			'top'
		);

	}
);

// rewrite /listing-category/slug to /slug -- conflicts with regular pages possible
add_filter(
	'term_link',
	function ( $url, $term, $taxonomy ) {

		if ( $taxonomy === 'hp_listing_category' ) {
			return home_url( '/' . $term->slug . '/' );
		}

		return $url;

	},
	10,
	3
);

add_action(
	'init',
	function () {

		add_rewrite_rule(
			'^([^/]+)/?$',
			'index.php?hp_listing_category=$matches[1]',
			'top'
		);

	}
);

I haven’t reviewed the code in detail as it was generated by AI, but I briefly tested it locally and it seems to work – I got URLs like “/real-estate/cozy-home” for listings and “/real-estate” for categories.

Hope this helps

1 Like

Yes, that PHP snippet worked to solve what i wanted in OP. Thank you.
I had to clear cache (using Wordpress caching plugin) and save that /wp-admin/options-permalink.php page 1-2 times. I am unsure about order of things, but if anyone have problem applying that PHP workaround, then try:

  1. enable PHP snippet inside “Code snippets” or “Fluent snippets” plugin,
  2. Save that Permalinks page (Maybe custom values on that Permalinks page can interfere)
  3. clear cache.
1 Like

Glad we could help resolve your request!

If you find HivePress useful, we would really appreciate it if you could take a moment to leave a review on the WordPress repository or Trustpilot. Your feedback means a lot to us and makes a real difference in helping us grow.