I have tried to search a solution but was unable to find one. In my wordpress website i've got a custom page that retrieves data from the database. Now i have this url:
http://www.domain.com/party/?title=nameoftheparty&id=4
I need to rewrite it to:
http://www.domain.com/party/nameoftheparty/4/
I tried to add the rewrite url in .htaccess but i get an 404 page.
What do i need to do?
Add both the code snippets to functions.php
1.We are telling WordPress that /party/nameoftheparty/4/ should be internally mapped to /party/?title=nameoftheparty&id=4
add_action( 'init', 'so27053217_init' );
function so27053217_init()
{
add_rewrite_rule(
'^party/([^/]*)/([^/]*)/?',
'index.php?pagename=party&title=$matches[1]&id=$matches[2]',
'top' );
}
This is optional and only required if you need to make use of title and id variables in your party page. They can be accessed using get_query_var("title")
add_filter( 'query_vars', 'so27053217_query_vars' );
function so27053217_query_vars( $query_vars )
{
$query_vars[] = 'title';
$query_vars[] = 'id';
return $query_vars;
}
Remember to re-save your permalinks to flush rewrite rules.
Related
I have been trying to get a WP url rewrite to work for the past few hours, but I can't seem to find what's wrong with it.
I have the below code which changes the post URLs to include a custom field value ('custom_id') and the post slug/name. The title change is working fine.
add_filter('post_type_link', 'change_post_type_link', 1, 3);
function change_post_type_link( $link, $post = 0 ){
if ($post->post_type == 'estate'){
return home_url('pand/'.get_field('custom_id',$post->ID).'/'.$post->post_name);
} else {
return $link;
}
}
Next, a custom WP rewrite rule is added that checks for the right post to be returned, but I always get a 404 page not found.
add_action( 'init', 'change_rewrites_init' );
function change_rewrites_init(){
add_rewrite_rule(
'pand/([0-9]+)/([a-z-]+)?$',
'index.php?post_type=estate&name=$matches[2]',
'top' );
}
Anyone have any clues why this isn't working? Thanks!
I am making a master/detail pattern sort of page in my theme.
When a person clicks a username, they will be taken to a page with that user's complete information. I did this by putting a link on the master page and then using $_GET to include said user's id in the url.
When i click the user name however, I am being redirected to index.php. I have tried add_rewrite_rule() but I'm still being redirected to index.In my functions.php I have the below code:
add_action( 'init', 'fwwp_rewrites_init' );
function fwwp_rewrites_init(){
add_rewrite_rule(
'user-profile/([0-9]+)/?$',
'index.php?pagename=user-profile&id=$matches[1]',
'top'
);
}
Then in my user-profile.php I have:
add_filter( 'query_vars', 'fwwp_query_vars' );
function fwwp_query_vars( $query_vars ){
$query_vars[] = 'id';
return $query_vars;
}
I even tried remove_filter('template_redirect','redirect_canonical');, so that i could at least get a 404, but I'm still being redirected.
What am I missing here?
I am trying to add a custom rewrite rule in the functions to handle some new features that I dont want to use query strings. I had this but it doesnt seem to be working
add_action( 'init', 'wpse26388_rewrites_init' );
function wpse26388_rewrites_init(){
add_rewrite_rule(
'books/([^/]+/?',
'index.php?pagename=books&book_name=$matches[1]',
'top' );
}
add_filter( 'query_vars', 'wpse26388_query_vars' );
function wpse26388_query_vars( $query_vars ){
$query_vars[] = 'book_name';
return $query_vars;
}
basically I want to catch site.org/books/ANYTHING/ and point it to another template and use the book_name query in there. But as of now anything after /books/ results in a 404 error and I am not sure how to pass in a page-template.php file to use.
I'm trying to add a rule for a custom post type. By default, the URL to view post is www.mydomain.com/job/post-slug
What I'd like, is the post also accessible with the following URL:
www.mydomain.com/j/postid
I've tried this in my functions.php file and I also refresh permalinks in admin settings:
function rewrite_short_job_url() {
add_rewrite_rule( '^j/([0-9]+)/?', 'index.php?post_type=job&id=$matches[1]', 'top' );
flush_rewrite_rules( true );
}
add_action( 'init', 'rewrite_short_job_url' );
Doesn't work for me, I'm trying to understand the Rewrite API but cannot find a solution.
I believe you should be using the following:
function rewrite_short_job_url() {
add_rewrite_rule( '^j/([0-9]+)/?', 'index.php?post_type=job&p=$matches[1]', 'top' );
}
add_action( 'init', 'rewrite_short_job_url' );
It's important to keep the post_type var, so that only job posts use that redirect.
You can view a list of WordPress query variables in the Codex.
I need to rewrite some URLs to custom templates. For example, I have a page at domain.com/page, and when a user clicks 'remove' on an element, it goes to domain.com/page/?remove=54. I'd like it to rewrite to domain.com/page/remove/54.
Help is appreciated, thank you in advance!
This exact code is untested, but it's derived from another instance of a similar situation that I did solve recently:
add_action( 'init', 'ss_permalinks' );
function ss_permalinks() {
add_rewrite_rule(
'page/remove/([^/]+)/?',
'index.php?pagename=page&service=$matches[1]',
'top'
);
}
add_filter( 'query_vars', 'ss_query_vars' );
function ss_query_vars( $query_vars ) {
$query_vars[] = 'removeid';
return $query_vars;
}
Re-save your permalink settings once after implementing. page is the slug of the page to point to when the user access this URL (domain.com/page/remove/432), and $matches[1] should be the number after remove/ in the URL. This number is accessible by the variable specified later, $query_vars[] = 'removeid';/ $removeid on the target page's template will be the number in the URL, if specified.