Want to redirect a Wordpress URL to a specific page template and pass name of post - wordpress

I've got a Wordpress site set up with a custom post type and custom taxonomy and custom URLs. However when I go to:
/custom-post-type/custom-taxonomy/custom-taxonomy-sub/postname
It 404s. I've tried everything and just want to create a redirect that grabs the postname and passes it to single-custom-taxonomy.php without changing the URL.
Some sort of redirect with a regex like /custom-post-type/(.*)/(.*)/(.*) and pass the third match to single-custom-taxonomy.php as a variable so it can get the post page.
How do I do this?

Figured it out:
function custom_rewrite_basic() {
add_rewrite_rule('^knowledge-center/(.*)/(.*)/(.*)', 'index.php?pagename=post_type=questions&questions=$matches[3]', 'top');
}
add_action('init', 'custom_rewrite_basic');

Related

add_rewrite_rule not working when referencing child page name as matches

Is anyone able to comment why this does not work.
I have created a custom post type called "knowledge-hub". I also then created a page called "knowledge-hub" to act as the landing page for my CPT listings, which I show using code. On one of the CPT pages, "minimum-wage-policies", I want to be able to access a variable in the URL, e.g. ?some_var=xyz. But I want it friendly like /knowledge-hub/minimum-wage-policies/xyz.
So I have created rewrite URLs. The page name "minimum-wage-policies" will vary for many of the CPT pages, so I cant hard code this. I want to access it using a URL part.
But I cant get the add_rewrite_rule() to match matches[1] to the page name. If I hard code this page name in the add_rewrite_rule it works! But as soon as I replace it with matches[1] it does not work, its either a 404 or a redirect back to minimum-wage-policies with no variable on the end.
URL would be:
http://my-domain.com/knowledge-hub/minimum-wage-policies/some_var
Referencing the child page name works:
add_rewrite_rule(
'knowledge-hub/([^/]*)?/([^/]*)/?$',
'index.php?post_type=knowledge-hub&pagename=minimum-wage-policies&my_var=$matches[2]',
'top'
);
matches[2] is working in this case as I can see the value passed in the URL in my code.
But using matches[1] does not work and returns a 404:
add_rewrite_rule(
'knowledge-hub/([^/]*)?/([^/]*)/?$',
'index.php?post_type=knowledge-hub&pagename=$matches[1]&my_var=$matches[2]',
'top'
);
Visiting the WP URL http://my-domain.com/index.php?post_type=knowledge-hub&pagename=minimum-wage-policies&my_var=xyz works, so that structure is OK.
I have tried various regex expressions used by various others on the internet, so not just ([^/])?. So am not sure if it is to do with the regex and I just have not found the right one. I want to match anything. Same issue with (.)
I also have this:
function add_my_vars($vars)
{
$vars[] = 'my_var';
return $vars;
}
add_filter('query_vars', 'add_my_vars');
Update:
Using $request->matched_query I could see the URLs being called.
When I use pagename=minimum-wage-policies the URL is as expected:
post_type=knowledge-hub&pagename=minimum-wage-policies&my_var=xyz
When I use matches[1] is completely changed which is quite bizarre:
knowledge-hub=minimum-wage-policies%2Fxyz&page=
I managed to get it working with this rewrite_rule but I have not found this structure online so not sure if its technically correct but it works. Note the use of knowledge-hub=$matches[1]. This was discovered from viewing the $request->matched_query I mentioned was bizarre above. Replicating it in the rewrite_rule makes it work.
add_rewrite_rule(
'knowledge-hub/([^/]*)?/([^/]*)/?$',
'index.php?knowledge-hub=$matches[1]&my_var=$matches[2]',
'top'
);

WordPress post permalinks for multiple categories, tags and taxonomy terms

I'm creating a WordPress plugin and one of the requirements is that a custom post type (photo) with a custom taxonomy (photos) needs to generate multiple permalinks if the post is assigned to multiple terms... so for example if a post is assigned to two taxonomy terms ("birthday" and "wedding") both of the following permalinks should display the post:
/photos/bithday/photo/post-name
/photos/wedding/photo/post-name
I currently have the following rewrite rule defined, which is returning a 404:
add_rewrite_rule( 'photos/(.*)/photo/(.*)?', 'index.php?post_type=photo&taxonomy=photos&term=$matches[1]&pagename=$matches[2]', 'top' );
However, if I visit the following URL which matches the pattern of my rewrite rule:
/index.php?post_type=photo&taxonomy=photos&term=birthday&pagename=post-name
It returns the correct content, but only after redirecting to this URL:
/photo/post-name/?taxonomy=photos&term=birthday-cakes
I must be missing something, any help would really be appreciated!
I managed to get this working by changing &pagename= to &name=.

Creating personalized URLs (PURLs) in Wordpress

I need to create a site that will allow for a unique user-identifying number to go in be appended to the URL.
So for instance
test.com/12345/about/
will load the page with slug 'about' (not redirect to it).
I have tried the following:
function custom_rewrite_basic() {
add_rewrite_rule('^([0-9]+)/', '/$matches[1]', 'bottom');
}
add_action('init', 'custom_rewrite_basic');
But it does not work, any prefix to the URL redirects the page with or without the block of code above.
So in my tests
test.com/23423423/about will re-direct to test.com/about which is not what I want.
Is what I am trying to achieve possible? If so, could anyone offer advice as to how I would best go about implementing it?

301 Redirect only Posts not Pages

I am looking to redirect all the posts and not pages and custom post types from old domain to new domain. So all posts for instance
http://www.example.com/post1 should go to http://example1.com/post1
http://www.example.com/post1 should go to http://example1.com/post1
The pages i.e. http://www.example.com/page1 should still be functional
I can find alot of answers on moving blog into sub directory along with moving all the links but not for moving just the posts and not the pages
Edit
There are over 1000 posts so I am looking for a dynamic way of doing this
Thank you for your help
The hack I finally end up using was
Add ob_start to single.php as first line even before get_header()
Add following code in functions.php
change example.com with your site domain name
add_action('wp_head','redirectme');
function redirectme() {
if ('post' === get_post_type()){
wp_redirect( 'http://example.com' . $_SERVER['REQUEST_URI'] , 301 ); exit;
}
}
I don´t think this is possible with this URL schema. .htaccess (or the Apache behind it) doesn´t know if post1 is a post or page. How shall it then redirect on this base?
If you would change the URL structure for pages alone (I don´t think this is possible by default, or am I wrong?) you could target their URL scheme and redirect.
To access the old pages (with the new URL) you could then use a "broken link checker" plugin.
Update: You could possibly use the hook described here to check wether the called post is a post or page and then use wp_redirect to call your new URL.

Wordpress custorm URL variables causes strange redirect

I'm trying to have custom variables in my URL for Wordpress site. I have read up as much as I could find on the subject and so far have the following in my functions page:
function add_query_vars($aVars) {
$aVars[] = "mrdrct";
return $aVars;
}
// hook add_query_vars function into query_vars
add_filter('query_vars', 'add_query_vars');
And the following on my header page:
if(isset($wp_query->query_vars['mrdrct'])) {
$mVar = $wp_query->query_vars['mrdrct'];
echo "variable is $mVar <br />";
}
Just to test out if things are being passed correctly and they are. However, when I use a link with the url variable in it - say www.mydomain.com/?mrdrct=myVarable - I am not directed to my homepage of my Wordpress site which is set to a static page with a template on it - I am instead directed to a page with my latest posts on it. I cannot figure out why this is happening - any ideas? Hopefully I've explained this well enough.
Thanks.
When WP sees a query string (? after the URL) it will attempt to display matching posts using it's rewrite rules. If no posts match it will show a 404 error - I would guess you do not have a 404.php file, so WP is showing the default which is index.php (see the Wordpress Template Hierarchy for more details on that).
I'm not 100% sure what you want to achieve, but I'd suggest that you need to look at changing the query when $wp_query->query_vars['mrdrct'] is set. See the WP Codex for query_posts() for a good place to start, if you are not already familier with it.

Resources