Remove cpt slug and add custom taxonomy in url - php

I need to change the custom post type url, this code works it but is also affecting other cpts causing 404 in all other cpt, how can I make it only for the one I need.
Here is my code
// Rewrite urls of resources and put category on url
function change_custom_post_link( $post_link, $id = 0 ){
$post = get_post($id);
if ( is_object( $post ) ){
$terms = get_the_terms( $post->ID, 'resource_type' );
if(get_post_type($post->ID) == 'resource' ) {
if( $terms ){
return home_url( "/".$terms[0]->slug."/".$post->post_name );
}else{
return home_url( "/resource/".$post->post_name );
}
}
}
return $post_link;
}
add_filter( 'post_type_link', 'change_custom_post_link', 1, 3 );
and this is the rewrite
function resource_rewrite_rules() {
add_rewrite_rule(
'^(.*)/(.*)/?$',
'index.php?post_type=resource&name=$matches[2]',
'top'
);
}
add_action( 'init', 'resource_rewrite_rules' );

Why are you doing this? That's too tricky.
You may use this plugin to create and modify CPT: CPT plugin WordPress
Or refer to docs: https://developer.wordpress.org/reference/functions/register_post_type/

Related

Force Wordpress to generate post slug from title only on specific custom post type

I use the code below to force WordPress to generate post slug from post title even when a user define a different slug or duplicate another slug but i want this to only affect a specific post type called job_listing but unfortunately it affects all post type on my site.
function myplugin_update_slug( $data, $postarr ) {
if ( ! in_array( $data['post_status'], array( 'draft', 'pending', 'auto-draft' ) ) ) {
$data['post_name'] = sanitize_title( $data['post_title'] );
}
return $data;
}
add_filter( 'wp_insert_post_data', 'myplugin_update_slug', 99, 2 );
How do i target only a specific post type called job_listing with the above code without affecting other post types
Can somebody help me amend this code to suit my need?
Your help will be appreciated.
According to the docs $data contains post_type so you should be able to use this:
function myplugin_update_slug( $data, $postarr ) {
if ( $data['post_type'] === 'job_listing' && ! in_array( $data['post_status'], array( 'draft', 'pending', 'auto-draft' ) ) ) {
$data['post_name'] = sanitize_title( $data['post_title'] );
}
return $data;
}
add_filter( 'wp_insert_post_data', 'myplugin_update_slug', 99, 2 );

add_rewrite_rule - suddenly stopped working (wordpress)

I have a url
example.com/sample-page/?amount=567
It initially worked ok, it redirects to:
example.com/sample-page/567
But after few days, i noticed its not working any more.
Its now redirecting to post without query var or get var
What could be the reason?? Is there any solution??
I used below codes in my functions.php file:
& I already flushed permalinks.
I am using custom post type
I have removed custom post type base from url using a plugin.
UPDATE
I just noticed Its due to empty post id/pagename.
So now, Question is how to get page name of current post...inside init function dynamically??
//https://developer.wordpress.org/reference/functions/wp_redirect/#comment-4109
add_action( 'init', function() {
global $wp;
$pagename = $wp->request;
add_rewrite_rule( ''.$pagename.'/([0-9]+)[/]?$', 'index.php?pagename='.$pagename.'&amount=$matches[1]', 'top' );
} );
add_filter( 'query_vars', function( $query_vars ) {
$query_vars[] = 'amount';
return $query_vars;
} );
function wpb_change_search_url() {
global $wp;
if ( ! empty( $_GET['amount'] ) ) {
wp_redirect( get_permalink( home_url( $wp->request ) ) . urlencode( get_query_var( 'amount' ) ) );
exit();
}
}
add_action( 'template_redirect', 'wpb_change_search_url' );

How to add multiple categories in function WordPress

Wordpress - functions.php
I'm using this function to automatically add custom structure in url for posts in a specific categorie.
But now I would like to know how I could add multiple categories without having to copy this function over and over.
Other solution could be to have this function work for the parent post category and all child categories.
// url structure
add_filter( 'post_link', 'custom_permalink', 10, 3 );
function custom_permalink( $permalink, $post, $leavename ) {
// Get the category for the post
$category = get_the_category($post->ID);
if ( !empty($category) && $category[0]->cat_name == "b" ) {
$cat_name = strtolower($category[0]->cat_name);
$permalink = trailingslashit( home_url('/questions/' . $post->post_name .'/' ) );
}
return $permalink;
}
add_action( 'init', 'custom_rewrite_rules' );
function custom_rewrite_rules() {
add_rewrite_rule(
'b/([^/]+)(?:/([0-9]+))?/?$',
'index.php?category_name=b&name=$matches[1]&page=$matches[2]',
'top' // The rule position; either 'top' or 'bottom' (default).
);
}

Custom permalink load template

I created functions below to customize the permalink of a specific category. Everything is working right!
When accessing the post with this permalink example.com.br/negocios/name-post everything is ok.
But if I access these same posts through example.com.br/name-post it also loads, being that it was for 404 or redirected to the created permalink.
add_filter( 'pre_post_link', 'idinheiro_custom_permalink', 10, 3 );
function idinheiro_custom_permalink( $permalink, $post ){
$category = get_the_category($post->ID);
$slug = $category[0]->slug;
$tag = '%postname%';
if ( !empty($category) && $slug === 'negocios' ) {
$permalink = "/$slug/$tag";
}
return $permalink;
}
add_filter( 'generate_rewrite_rules', 'idinheiro_custom_rewrite_rules' );
function idinheiro_custom_rewrite_rules( $wp_rewrite ) {
$new_rules = [
'^negocios/([^/]+)/?$' => 'index.php?name=$matches[1]',
];
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
return $wp_rewrite->rules;
}

Wordpress/WoOCommerce Changed search-slug: Want to search/show results from specific custom type

Search is set via Relevanssi plugin to only search for the custom type "brands". However, using this code beneath to change the url slug makes it search everything except custom types. How can I alter the code to search specifically in the "brands" custom type?
function change_sok_url() {
if ( is_search() && ! empty( $_GET['s'] ) ) {
wp_redirect( home_url( "/sok/" ) . urlencode( get_query_var( 's' ) ) );
exit();
}
}
add_action( 'template_redirect', 'change_sok_url' );
function rewrite_sok_slug() {
add_rewrite_rule(
'sok(/([^/]+))?(/([^/]+))?(/([^/]+))?/?',
'index.php?s=$matches[2]&paged=$matches[6]',
'top'
);
}
add_action( 'init', 'rewrite_sok_slug' );

Categories