URL Rewriting if it is a single post / redirecting post – post type

First of all set your permalink structure to /%postname%. After that add a rewrite rule to rewrite all request like example.com/blog/postname to the appropriate query: add_action(‘init’, ‘blog_front’); function blog_front() { add_rewrite_rule(‘^blog/([^/]+)/?’,’index.php?name=$matches[1]’,’top’); } Now you have to prevent the duplicate url, sending a 301 redirect when example.com/postname is requested: add_action(‘template_redirect’, ‘say_hello_to_google’); function say_hello_to_google() { if ( … Read more

Adding pretty query parameters

You would need a custom rewrite rule for that: http://codex.wordpress.org/Rewrite_API/add_rewrite_rule I think you might have a hard time though, with it being within an area of the site (single post type) already using a custom rewrite rule!

Checking that the Rewrite API is available

You want WP_Rewrite::using_permalinks. <?php add_action(‘init’, ‘wpse142273_add_rules’); function wpse142273_add_rules() { global $wp_rewrite; if ($wp_rewrite->using_permalinks()) { // Plan A } else { // Plan B } } Here’s a larger example. <?php // inside a class somewhere public static function url($area, $additional=null) { global $wp_rewrite; if ($wp_rewrite->using_permalinks()) { $path = “/account/{$area}”; if ($additional) { $path .= “https://wordpress.stackexchange.com/” … Read more

Query var removed after rewrite

Maybe you are not using the correct hooks nor the correct rewrite rule. You are rewriting to pagename, which is for pages. You should use name to get posts by slug. add_filter(‘query_vars’, ‘cyb_add_query_vars’); function cyb_add_query_vars( $vars) { $vars[] = “item”; return $vars; } add_action(‘init’,’cyb_add_rewrite_rules’); function cyb_add_rewrite_rules() { add_rewrite_rule( ”^shows/([^/]+)/([^/]+)/?$” , ‘index.php?post_type=shows&name=$matches[1]&item=$matches[2]’ , ‘top’ ); } … Read more

Conditionally custom post type url rewrite

Yes it is possible. One part of my question is answered here and full code I will type here. I have larger function wich checkes post taxanomy (in this case custom categories for custom post type). Then we check is there particular taxonomy and we change URL structure. add_filter(‘post_type_link’, ‘replace_link’, 1, 3); function replace_link( $link, … Read more

Dynamically generate child-URLs for posts or custom posts

I just completed something like this (with great help from folks on this site). First, you need to add the rewrite endpoint to your functions: function wpa_read_endpoint(){ add_rewrite_endpoint( ‘sub-url’, EP_PERMALINK); } add_action( ‘init’, ‘wpa_read_endpoint’ ); Make sure to then go to the permalinks section of admin and save to refresh permalink settings. Then add some … Read more