Ask your WordPress questions! Pay money and get answers fast! (more info)

Rewrite Rules for single category, getting 404

I have a site where the current URL structure is: <strong>url / postname</strong>

I need to make only one category of the blog (the 'blog' category) have <strong>blog</strong> added to the URL structure, ie. <strong>url / blog / postname</strong>

I don't want <strong>blog</strong> in the URL structure for other posts not in the blog category, or for other custom post types, thus I can't add <strong>/blog/%postname%/ </strong>to the permalinks page.

I've currently got this code added to <strong>functions.php</strong> - which results in the correct URL, but the page gives a 404, and yes I did visit the permalinks page to clear the permalinks cache.


// filter permalinks to add /blog/ to blog posts
add_filter( 'post_link', 'custom_permalink', 10, 2 );
function custom_permalink( $permalink, $post ) {
// Get the categories for the post
$categories = wp_get_post_categories( $post->ID );
foreach ( $categories as $cat ) {
$post_cat = get_category( $cat );
$post_cats[] = $post_cat->slug;
}

// Check if the post have 'blog' category
// Assuming that your 'Blog' category slug is 'blog'
// Change 'blog' to match yours
if ( in_array( 'blog', $post_cats ) ) {
$permalink = trailingslashit( home_url( 'blog/' . $post->post_name ) );
}

return $permalink;
}


Here is a link to the site in question: http://bit.ly/21r773n

The first three posts are in the blog category and are showing the the URL: sitename.com/blog/test but giving a 404.

Any idea what I'm missing to stop that 404 happening?

Answers (4)

2016-02-24

Tache Madalin answers:

After adding that code to your functions did you resave your permalinks?


Mike Sewell comments:

Yes.

2016-02-24

Reigel Gallarde answers:

add this code...

add_action( 'wp_loaded', 'my_permastructure' );
function my_permastructure() {
global $wp_rewrite;
add_permastruct( 'blog', 'blog/%postname%', false );
}


then please visit Dashboard > Settings > Permalinks... or wp-admin/options-permalink.php to activate effect...


Mike Sewell comments:

This works but I'm now getting a 404 for page 2 of pagination: http://bit.ly/1Qf5Dpa


Reigel Gallarde comments:

ok use this code instead...

add_action( 'wp_loaded', 'my_permastructure' );
function my_permastructure() {
global $wp_rewrite;
add_permastruct( 'main_blog', 'blog', array('paged'=>true) );
add_permastruct( 'blog_post', 'blog/%postname%');
}


again, please visit Dashboard > Settings > Permalinks... or wp-admin/options-permalink.php to activate effect...


Mike Sewell comments:

Thanks for the update - now what's happening is if I click on any of the pagination links I'm taken back to the homepage instead of page 2, etc. I did visit permalinks page again as well.

http://bit.ly/21r773n


Reigel Gallarde comments:

what is http://yoursite.com/blog/ ? is this a page?


Mike Sewell comments:

Yes, it's the main blog page - the one that shows posts from Settings > Reading


Reigel Gallarde comments:

is your settings like this ? [[LINK href="http://i.imgur.com/e9JyEgn.png"]]http://i.imgur.com/e9JyEgn.png[[/LINK]]


Mike Sewell comments:

Yes, exactly like that.


Reigel Gallarde comments:

this I'm not sure... but your blog page is being treated as category... [[LINK href="http://i.imgur.com/eFkvbd1.png"]]http://i.imgur.com/eFkvbd1.png[[/LINK]]

it should not have cat-5-id in there... that's a class for category with id of 5...


Mike Sewell comments:

The query for the blog page has to remove category 5, could that be the cause?


<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts("showposts=9&cat=-5&paged=$paged");
?>


Reigel Gallarde comments:

hmm not sure, but what I wanted to know now is if "blog" was created as a page and using template?


Mike Sewell comments:

Yes, I have a static home page and the blog page is a standard WP page set to blog in the Settings > Reading section.
The query it is running is comeing from home.php - would you like me to post that code?

2016-02-25

Bob answers:

What is slug of <strong>blog page</strong> itself ?
What is slug of <strong>blog category</strong>?


Mike Sewell comments:

That could be the problem, they are both 'blog'


Bob comments:

Yes that is problem

when two different items in wordpress have same slug it always creates trouble.

2016-02-25

Andrea P answers:

Hi Guys!

@Reigel, that probably happens because he also have a category with slug "blog". so when wordpress sees the url of the pages, it assumes that page/2/ is the title of a post under the blog category.

he should change the slug of that category, so that is something like "blog-posts" or "articles"