WP add_rewrite_rule with custom field and post name - wordpress

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!

Related

How to remove the original link of a post in Wordpress after using post_link and add_rewrite_rule to change the link?

I changed the permalinks of my posts using post_link, adding "/test/" to it.
So for example www.website.com/this-is-fun/ is now having the permalink www.website.com/test/this-is-fun/
This permalink is correctly showing in the admin, when editing a post, and also on the website the link to the post is the new one.
However, when I open this link it is not working, I get a 404. I was able to fix that with a add_rewrite_rule, but now my problem is that BOTH urls exist. I want only the new one to exist.
Am I making a mistake in the post_link part, is there a way you can automatically disable the original link and make the new one work? Or should I use add_rewrite_rule to disable the original? Or is there another way?
This is my code:
add_filter( 'post_link', 'custom_permalink', 10, 3 );
function custom_permalink( $permalink, $post, $leavename ) {
$permalink = home_url('/test/' . $post->post_name .'/' );
return $permalink;
}
add_action( 'init', 'custom_rewrite_rules' );
function custom_rewrite_rules() {
add_rewrite_rule(
'test/([^/]+)(?:/([0-9]+))?/?$',
'index.php?name=$matches[1]',
'top'
);
}
add_action( 'init','flush_rewrite_rules' );
I know I can change the permalink in the settings btw, but this new permalink will not be for all posts, so I need to do it this way.

Add Rewrite Rule for Single Custom Post Type

I'm having trouble creating a custom URL for certain single post types. whereas it works for posts. Here is my code to change the URL of only certain posts:
/**
* Rewire the permalink of individual posts so that they direct to the Free Cakes section
*/
add_filter( 'post_link', 'ae_replace_freecakes_posts_link', 90, 2 );
function ae_replace_freecakes_posts_link( $permalink, $post ) {
global $freecakes_blog_page_id;
if ( ae_get_post_acces_level( $post->ID ) >= 110 ) {
return get_permalink( $freecakes_blog_page_id ) . "$post->ID/{$post->post_name}/";
}
return $permalink;
}
/**
* Rewire the permalink of individual videos so that they direct to the Free Cakes section
*/
add_filter( 'post_type_link', 'ae_replace_freecakes_videos_link', 90, 2 );
function ae_replace_freecakes_videos_link( $permalink, $post_object ) {
global $freecakes_video_page_id;
if ( $post_object->post_type != 'video' ) {
return $permalink;
}
if ( ae_get_post_acces_level( $post_object->ID ) >= 110 ) {
return get_permalink( $freecakes_video_page_id ) . "$post_object->ID/{$post_object->post_name}/";
}
return $permalink;
}
And later this:
add_action( 'init', 'ae_add_rewrite_rules' );
function ae_add_rewrite_rules() {
add_rewrite_rule( "^freecakes/video/([0-9]+)/([^/]*)/?$", 'index.php?p=$matches[1]', 'top' );
add_rewrite_rule( "^freecakes/posts/([0-9]+)/([^/]*)/?$", 'index.php?p=$matches[1]', 'top' );
}
The problem is that this doesn't work for videos. If I use this fictional exemplary url it works just fine:
https://mywebsite.com/freecakes/posts/3892/my-first-frecake-post/
But this URL (the one matched to the video custom post type) gives a 404 error:
https://mywebsite.com/freecakes/posts/10109/my-first-frecake-video/
Any clues of what might be my mistake here? Thank you!
Did you flush and regenerate the rewrite rules database after modifying rules. From WordPress Administration Screens, Select Settings -> Permalinks and just click Save Changes without any changes.
As it turns out, even though calling https://mywebsite.com/index.php?p=10109 would normally work while using the navigation bar, it doesn't work for custom post types when using add_rewrite_rule
The rule that works is:
add_rewrite_rule( "^freecakes/posts/([0-9]+)/([^/]*)/?$", 'index.php?video=$matches[2]', 'top' );
Where $matches[2] is the slug of the custom post type (in this case a post_type called "video")

Wordpress Custom Rewrite Rule in Functions.php

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.

WordPress custom post type rewrite rule

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.

Wordpress URL rewrite with GET variables

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.

Resources