How do I know if a rewritten rule was applied?

I’m not very sure of what you’re trying to achieve, but I hope this answer can help. 🙂 With the way your custom post type is being registered, WordPress will create custom rewrite rules such as below for the CPT’s single post pages: RegEx: ballinfo/([^/]+)(?:/([0-9]+))?/?$ Query: index.php?balls=$matches[1]&page=$matches[2] So for the following question: how do I … Read more

How can I create custom URL routes?

Add this to your theme’s functions.php, or put it in a plugin. add_action( ‘init’, ‘wpse26388_rewrites_init’ ); function wpse26388_rewrites_init(){ add_rewrite_rule( ‘properties/([0-9]+)/?$’, ‘index.php?pagename=properties&property_id=$matches[1]’, ‘top’ ); } add_filter( ‘query_vars’, ‘wpse26388_query_vars’ ); function wpse26388_query_vars( $query_vars ){ $query_vars[] = ‘property_id’; return $query_vars; } This adds a rewrite rule which directs requests to /properties/ with any combination of numbers following to … Read more

Custom Post Type Advanced Slug

This is pretty straightforward using a rewrite endpoint. The first step is to set an ep_mask when you register your taxonomy, which is part of the rewrite argument. Note that it’s a constant, and should be as-is, without quotes: $args = array( ‘rewrite’ => array( ‘slug’ => ‘location’, ‘with_front’ => false, ‘ep_mask’ => EP_TAGS ), … Read more

Programmatically add custom field to post_name in a custom post type

You can filter the slug creation on wp_unique_post_slug hook. //Register the filter add_filter(‘wp_unique_post_slug’,’prefix_the_slug’,10,6); function prefix_the_slug($slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug){ //Get value from the custom field $prefix = get_post_meta($post_ID,’cw_event_start_date’,true); //Only prefix certain post type and if prefix field existed if($post_type===’post’ && $prefix && !empty($prefix)){ //Prefix only if it is not already prefixed preg_match (‘/^\d\d\d\d\d\d/’, $slug, … Read more

Add term slug in URL of custom post type details page

You need to update below line at where you have register a custom post type using register_post_type function. ‘rewrite’ => array(‘slug’ => ‘movies/%cat%’) To change permalink dynamically of post type you have to add below code in functions.php file : function change_link( $post_link, $id = 0 ) { $post = get_post( $id ); if( $post->post_type … Read more

How to rewrite wordpress search to work on specific category

You can use add_rewrite_rule(): add_action( ‘init’, function(){ // Non-paged requests. E.g. example.com/categoryname/search/keyword/ add_rewrite_rule( ‘^categoryname/search/([^/]+)/?$’, ‘index.php?category=categoryname&s=$matches[1]’, ‘top’ ); // For paged requests. E.g. example.com/categoryname/search/keyword/page/2/ add_rewrite_rule( ‘^categoryname/search/([^/]+)/page/(\d+)/?$’, ‘index.php?category=categoryname&s=$matches[1]&paged=$matches[2]’, ‘top’ ); } ); Be sure to flush the permalinks — simply visit the Permalink Settings page.