• Resolved stevecoy

    (@stevecoy)


    I have a custom post type, Videos. It uses standard WP taxonomies (categories, tags).

    $videoargs = array(
    		'labels' => $labels,
    		'public' => true,
    		'publicly_queryable' => true,
    		'show_ui' => true,
    		'show_in_menu' => true,
    		'query_var' => true,
    		'rewrite' => array('slug' => 'video', 'with_front' => 'false'),
    		'has_archive' => 'videos',
    		'hierarchical' => false,
    		'capability_type' => 'post',
    		'menu_position' => 20,
    		'menu_icon' => $icon,
    		'taxonomies' => array('category'),
    		'supports' => array('title','thumbnail','excerpt','comments')
    	  ); 
    
    	register_post_type( 'li_video', $videoargs);

    The archive page for viewing all videos is at mysite.com/videos, using theme file archive-li_videos.php. I would like to be able to view different categories, showing videos only, at a URL like this:

    mysite.com/videos/my-category-name
    OR
    mysite.com/videos/category/my-category-name

    I know this is possible, because I’ve seen a plugin do it, but said plugin has really huge architecture and I can’t figure it out.

    I know I could easily pass a query string parameter like mysite.com/category/my-category-name?view=videos and alter the query parameters accordingly, but I’d like to avoid this. I also do not want to register a separate taxonomy for my custom post type, because then users will have an extra thing to keep track of. And it seems like this is doable.

    Thanks for your help!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    It is doable by using the Rewrite API and adding rewrite rules. I can’t tell you exactly what to do. My rules don’t always work and I have trouble determining why. Other times my attempts work fine. I’m still missing something here, but I know enough to know this is the approach you need.

    mysite.com/videos/my-category-name will be a problem because the parser will not know if the URL is for a video post or a category query. mysite.com/videos/category/my-category-name should be no problem.

    Thread Starter stevecoy

    (@stevecoy)

    bcworkz, thanks for the help. I got it working by putting this in functions.php:

    add_action( 'init', 'video_rewrite_rule');
    function video_rewrite_rule() {
    	add_rewrite_tag('%videos%','true');
    	add_rewrite_tag('%category%','([^&]+)');
    	add_rewrite_tag('%li_video%','([^&]+)');
    	add_rewrite_rule( '^videos/category([^/]*)/([^/]*)/?', 'index.php?videos=true&category=$matches[2]&li_video=$matches[3]', 'top');
    	//flush_rewrite_rules(); leave this in once and then comment it out
    }
    
    add_action( 'pre_get_posts', 'video_query_edit',1);
    
    function video_query_edit($query){
    	if (!empty($query->query_vars['videos']) && isset($query->query_vars['category'])) {
    	$query->set('post_type', 'li_video' );
    	//$query->set('posts_per_page', 10 );
    	$query->set('category_name',$query->query_vars['category']);
    	}
    }
    
    add_filter('template_redirect', 'video_template_redirect');
    
    function video_template_redirect(){
    //this is what tells the page to use the archive-li_video.php in the theme, you could also do archive.php or whatever
    
     global $wp_query;
    
        if (!empty($wp_query->query_vars['videos']) && isset($wp_query->query_vars['category'])) {
            include( get_template_directory() . '/archive-li_video.php' );
    		exit;
        }
      }

    I got part of this from this article: https://pippinsplugins.com/wordpress-rewrite-api-part-2/ (costs $6 to be able to view it)

    You can obviously modify the particulars to use different custom post types. There, I just saved the WP community $6 and countless hours!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Category permalinks with custom post types’ is closed to new replies.