WP Rewrite the last two parts of the URL

I have finally solved my rewrite rule problem. I used the class outlined in the answer of this wordpress.stackoverflow question. After including that class I used the following code to make it work: // These query vars can be retrieved on the page by using get_query_var( ‘queryvar1’ ); $options = array( ‘query_vars’ => array( ‘queryvar1’, … Read more

Why does rewrite rule work for page not for custom post type post?

Well, I found the solution. It seems that, when applying a rewrite rule on a custom post type post, one needs to tell WP the custom post type’s name within the rewrite rule itself. Here’s the code that does NOT work (redirects and drops parameter variables instead of rewrite): function add_rewrite_rules($rules) { $newrules = array(‘cpt-slug/([^/]+)/([^/]+)/?$’ … Read more

Rewrite url / permalink for default archive – yearly / monthly

You can do this without adding rewrite rules by changing the $date_structure of the $wp_rewrite instance of the WP_Rewrite class: function wpa116030_init(){ global $wp_rewrite; $wp_rewrite->date_structure=”about/blog/archive/%year%/%monthnum%/%day%”; } add_action( ‘init’, ‘wpa116030_init’ ); Visit your Permalinks settings page after adding this code to flush the rewrite rules.

Add_query_arg + two times the same argument?

Nilambar’s comment solved this for me. “You can pass two values as comma separated in a single parameter events. And later parse value correctly in your page. – Nilambar” I use this to get posts with tag1 OR tag2: echo ‘<a href=”‘.esc_attr(add_query_arg( ‘events’, ‘tag1,tag2′)).'”>Linkname</a>’; And to get all posts with tag1 AND tag2 simply use … Read more

Remove child category from URL

You can use the post_link_category filter to remove child categories from permalinks: function wpse147453_remove_child_categories_from_permalinks( $category ) { while ( $category->parent ) { $category = get_term( $category->parent, ‘category’ ); } return $category; } add_filter( ‘post_link_category’, ‘wpse147453_remove_child_categories_from_permalinks’ );

Use the plus sign “+” instead of dash “-” in URLs

Mark Jaquith uses “+” as the search query string delimiter in his Nice Search Plugin. Here’s what he does: function cws_nice_search_redirect() { if ( is_search() && strpos( $_SERVER[‘REQUEST_URI’], ‘/wp-admin/’ ) === false && strpos( $_SERVER[‘REQUEST_URI’], ‘/search/’ ) === false ) { wp_redirect( home_url( ‘/search/’ . str_replace( array( ‘ ‘, ‘%20’ ), array( ‘+’, ‘+’ ), … Read more