WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Custom post type rewrite and custom rewrite rules (2 posts)

  1. wpismypuppet
    Member
    Posted 2 years ago #

    So... I'm currently working on a site that has a custom post "wbc_project" with a custom taxonomy "wbc_project_category". The custom taxonomy has a rewrite rule:

    'rewrite' => array( 'slug' => 'projects', 'with_front' => false )

    and the custom post has a rewrite rule:

    'rewrite' => array('slug'=>'projects%wbc_project_category%','with_front'=>false)

    Using the following, I am able to dynamically populate the main navigation, footer navigation, and internal page navigation:

    `	// Function to get the full url
    	function selfURL() {
    		$s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : "";
    		$protocol = strleft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/").$s;
    		$port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);
    		return $protocol."://".$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI'];
    	}
    	function strleft($s1, $s2) { return substr($s1, 0, strpos($s1, $s2)); }
    	// Function to swap out correct project category name in the permalink
    	add_filter('post_type_link', 'project_category_permalink', 10, 4);
    	function project_category_permalink($post_link, $post, $leavename, $sample) {
    		$project_category == '';
    		if(false !== strpos($post_link, '%wbc_project_category%')) {
    			$url = selfURL();
    			if(stripos($url, 'projects/') !== false) {
    				$project_category = substr($url, stripos($url, 'projects/') + 9);
    				if(stripos($project_category, '/') !== false) {
    					$project_category = substr($project_category, 0, stripos($project_category, '/'));
    				}
    				$project_category = '/'.$project_category;
    			}
    			$post_link = str_replace('%wbc_project_category%', $project_category, $post_link);
    		}
    		return $post_link;
    	}`

    When you go to "/projects" you get a list of all the wbc_project_category taxonomies that have wbc_project posts assigned to them. When you visit any of the "/projects/wbc_project_category you get a list of all the posts tagged with this custom taxonomy. The problem lies when you click on any one of the posts from here! You get a 404.

    Now the reason this is set up this way to begin with, is if I visit one of the wbc_project posts, I want the url to reflect what wbc_project_category you were visiting when you clicked the wbc_project post. Since these posts can be associated with more than one wbc_project_category, this becomes relevant. Any suggestions as to how to make this work without 404s?

    Is this because the custom post and custom taxonomy are both trying to rewrite to "projects" even though the custom post has a %wbc_project_category% after it?

    If this requires a custom rewrite rule, any suggestions on that would be appreciated as well as I am not too familiar with WordPress rewrite rules. Sorry this is so long winded, but I wanted to be as clear as I thought I could be.

    You can check out the site here: http://williamsbuilding.designprinciples.com/projects but be aware the client is updating projects and there are some images and whatnot missing due to their effort.

  2. wpismypuppet
    Member
    Posted 2 years ago #

    Figured it out... needed to write my own rewrite rule. So basically all I changed was on my custom post type "wbc_project" I added:

    'rewrite' => false,
    'publicly_queryable' => true,
    'query_var' => true

    Not sure if publicly_queryable is necessary, but the other two definitely are. Then I needed to add this bit of code:

    global $wp_rewrite;
    $project_structure = '/projects/%wbc_project_category%/%wbc_project%';
    $wp_rewrite->add_rewrite_tag("%wbc_project%", '([^/]+)', "wbc_project=");
    $wp_rewrite->add_permastruct('wbc_project', $project_structure, false);

    I left everything else the same... went into WordPress->settings->permalinks and saved that without making changes (suppose I could have used a flush_rewrite(), but this was easier) and all is working.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.