How can I replace a custom-post-type with a custom post category in permalink without getting a 404 in wordpress? - wordpress

I'm a wordpress newbie. I will try to describe my problem in the clearest way possible.
I'm trying to do two things here:
Remove CPT from a permalink.
Add a custom taxonomy type where it used to be the CPT in the permalink.
Permalinks on site used to be like this:
http://example.com/custom-post-type/post-name
I managed to remove the CPT from the permalink based on this:
how to remove custom post type from wordpress url?
Then I modify my code to add the custom taxonomy type to the permalink to be like this:
http://example.com/post-category/post-name
This is my code:
function remove_cpt_slug( $post_link, $post ) {
if ( 'custom-post-type-name' === $post->post_type && 'publish' === $post->post_status ) {
$post_tags = get_the_terms($post->ID, 'custom-post-type-name-category');
$post_link = str_replace( '/' . $post->post_type . '/', '/' . $post_tags[0]->slug . '/', $post_link );
}
return $post_link;
}
add_filter( 'post_type_link', 'remove_cpt_slug', 10, 2 );
function add_cpt_post_names_to_main_query( $query ) {
// Return if this is not the main query.
if ( ! $query->is_main_query() ) {
return;
}
// Return if this query doesn't match our very specific rewrite rule.
if ( ! isset( $query->query['page'] ) || 2 !== count( $query->query ) ) {
return;
}
// Return if we're not querying based on the post name.
if ( empty( $query->query['name'] ) ) {
return;
}
// Add CPT to the list of post types WP will include when it queries based on the post name.
$query->set( 'post_type', array( 'post', 'page', 'custom-post-type-name' ) );
}
add_action( 'pre_get_posts', 'add_cpt_post_names_to_main_query' );
This worked but now every post from my CPT gives a 404. How can I solve this?

Related

WordPress default search and Woocommerce product search

I have a problem when trying to get the search to work for both the default WordPress blog search and WooCommerce product search and I can't seem to find the solution anywhere. What I have in functions.php right now is:
function wp_search_filter($query) {
if ( $query->is_search ) {
$query->set( 'post_type', 'post' );
}
if ( function_exists( 'is_woocommerce' ) ) :
if ( $query->is_search && is_woocommerce() ) {
$query->set( 'post_type', 'product' );
}
endif;
return $query;
}
add_filter('pre_get_posts','wp_search_filter');
But my product search does not work. Any ideas?
To answer my own question, in case anyone needs a solution, I just came up with something that works. The problem here was that the is_woocommerce() condition is not firing on search results template. To get around that I used this code:
function wp_search_filter($query) {
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if ( (strpos($url,'post_type=product') !== false) && is_search() ) {
$query->set('post_type', 'product');
}
return $query;
}
add_filter('pre_get_posts','wp_search_filter');

remove a particular custom post type slug from the url in wordpress

How to remove a particular custom post type slug from the url in wordpress 4.7
here is that link
http://dmstage.com/gardencity/projects/bridge-construction/
But i need it should come like this
http://dmstage.com/gardencity/bridge-construction/
the projects should be removed from the link..I just tried to rewrite it in the functions.php..But couldnot get the correct way to do it..
Thanks in advance..
Try creating this function :
function na_remove_slug( $post_link, $post, $leavename ) {
if ( 'events' != $post->post_type || 'publish' != $post->post_status ) {
return $post_link;
}
$post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
return $post_link;
}
add_filter( 'post_type_link', 'na_remove_slug', 10, 3 );

Wordpress Permalink Generating 404 Page

I'm working on changing the permalink of a Custom Post Type to include the taxonomy prior to the post id. I'm capable of it displaying the taxonomy in the URL however when I go to the page I get a 404 Error. It looks like the structure of the permalink is correct however the location of the post isn't synced up w/ the database location.
Any help is appreciated and thanks in advance!
Couple of notes:
My .htaccess file has mod rewrite on.
I've added %tax% to the permalink rewrite for the CPT
I have archive turned on for the CPT
Code
function change_permalink( $link, $post ) {
if ( ‘custom-post-type-name’ == get_post_type( $post ) ) {
// Get post
$post = get_post($post_id);
if (!$post) return $permalink;
// Get taxonomy terms
$terms = wp_get_object_terms($post->ID, ’taxonomy-name’);
if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) $taxonomy_slug = $terms[0]->slug;
else $taxonomy_slug = 'no-taxonomy-listed’;
return str_replace('%tax%', $taxonomy_slug, $link);
}
return $link;
}
add_filter( 'post_type_link', ‘change_permalink’, 10, 2 );
Figured it out. Needed to wp_rewrite:
add_action( 'wp_loaded', 'urban_permastructure' );
function urban_permastructure($post) {
if ( 'custom-post-type-name' == get_post_type( $post ) ) {
global $wp_rewrite;
$structure = '/cat/%tax%';
$wp_rewrite->add_rewrite_tag("%tax%", '([^/]+)', "tax-type=");
$wp_rewrite->add_permastruct('tax-type', $structure, false);
}
}

How to remove taxonomy slug from custom post type url?

I have a custom post type(product) with taxonomy product-type. One of my url like this:
http://www.naturesbioscience.com/product-type/immune-support-supplements/
I want this like:
http://www.naturesbioscience.com/immune-support-supplements/
I have used "rewrite" => array('slug' => '/ ', 'with_front' => false in register_taxonomy function and I got the url like:
http://www.naturesbioscience.com/immune-support-supplements/
But I got 404 not found in other pages.
Anyone can help me?
I think you forgot to rewrite custom taxonomy post slug.
Write this in your register_post_type methord.
'rewrite' => array('slug' => 'product-type')
Now you have to remove product-type slug from your custom products
/**
* Remove the slug from published post permalinks.
*/
function custom_remove_cpt_slug($post_link, $post, $leavename)
{
if ('product-type' != $post->post_type || 'publish' != $post->post_status)
{
return $post_link;
}
$post_link = str_replace('/' . $post->post_type . '/', '/', $post_link);
return $post_link;
}
add_filter('post_type_link', 'custom_remove_cpt_slug', 10, 3);
Now as you have removed the custom post type slug so WordPress will try to match it with page or post so you have tell WP to check the URL in you custom post type also. So use this for that:
function custom_parse_request_tricksy($query)
{
// Only noop the main query
if (!$query->is_main_query())
return;
// Only noop our very specific rewrite rule match
if (2 != count($query->query) || !isset($query->query['page']))
{
return;
}
// 'name' will be set if post permalinks are just post_name, otherwise the page rule will match
if (!empty($query->query['name']))
{
$query->set('post_type', array('post', 'product-type', 'page'));
}
}
add_action('pre_get_posts', 'custom_parse_request_tricksy');
Reference: Remove The Slugs from Custom Post Type URL
Hope this helps!
add_filter( 'post_type_link', 'change_product_request', 10, 3 );
function change_product_request( $post_link, $post, $leavename ) {
if ( 'product' != $post->post_type || 'publish' != $post->post_status ) {
return $post_link;
}
$post_link = str_replace( "/{$post->post_type}/" , '/', $post_link );
return $post_link;
}
Now, you'll get a 404 page because WordPress only allow posts and pages to behave in this way. You'll also have to add the following:
add_action( 'pre_get_posts', 'product_permalink' );
function product_permalink( $query ) {
if ( ! $query->is_main_query() ) {
return;
}
if ( 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
return;
}
if ( ! empty( $query->query['pagename'] ) ) { // name has been updated to pagename so $query->query['pagename']
global $wpdb;
$pt = $wpdb->get_var(
"SELECT post_type FROM `{$wpdb->posts}` " .
"WHERE post_name = '{$query->query['pagename']}'"
);
$query->set( 'post_type', $pt );
}
return $query;
}
I had similar issue. The question is quite old, but anyone looking for apt answer can see this.
Do not pass "/" for rewrite slug, since it causes more problem than it solves, as in this case, causing 404 error in other pages.
First, we need to remove the slug from the URL for the published post. Paste the code in functions.php
/**
* Remove the slug from published post permalinks. Only affect our CPT though.
*/
function sh_remove_cpt_slug( $post_link, $post, $leavename ) {
if ( in_array( $post->post_type, array( 'product-type' ) )
|| 'publish' == $post->post_status )
$post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
return $post_link;
}
add_filter( 'post_type_link', 'sh_remove_cpt_slug', 10, 3 );
This will still cause error since it specifies that only 'post' and 'page' post types can have url without post-type slug.
Now to teach WP that out CPT will also have URL without slug, we need to get this in our functions.php
function sh_parse_request_tricksy( $query ) {
// Only loop the main query
if ( ! $query->is_main_query() ) {
return;
}
// Only loop our very specific rewrite rule match
if ( 2 != count( $query->query )
|| ! isset( $query->query['page'] ) )
return;
// 'name' will be set if post permalinks are just post_name, otherwise the page rule will match
if ( ! empty( $query->query['name'] ) ) {
$query->set( 'post_type', array( 'product-type' ) );
}
}
add_action( 'pre_get_posts', 'sh_parse_request_tricksy' );
This is it. Ref: https://wordpress.stackexchange.com/a/320711/98322

How to replace the custom post slug by the category name in wordpress

I need to change the permalink struture of my custom post "product"
the default one is Domain_name.com./product/product_name
I need to change it to :
Domain_name.com./category_name/product_name
i used the plugin "Types" to create the custom post
i installed a plugin named wp-permastructure to change the permalink but
when i add %category%/%postname%/ in the new format structure i get a 404 error.
i add the follow code to remove the product slug :
function gp_remove_cpt_slug( $post_link, $post, $leavename )
{
if ( 'product' != $post->post_type || 'publish' != $post->post_status ) {
return $post_link;
}
$post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link);
return $post_link;
}
add_filter( 'product', 'gp_remove_cpt_slug', 10, 3 );
function gp_parse_request_trick( $query ) {
// Only noop the main query
if ( ! $query->is_main_query() )
return;
// Only noop our very specific rewrite rule match
if ( 3!= count( $query->query ) || ! isset( $query->query['page'] ) ) {
return;
}
// 'name' will be set if post permalinks are just post_name, otherwise the page rule will match
if ( ! empty( $query->query['name'] ) ) {
$query->set( 'post_type', array( 'post', 'product', 'page' ) );
}
}
add_action( 'pre_get_posts', 'gp_parse_request_trick' );
But i still get the 404 error
have you some ideas for my problem

Resources