Woocommerce product ID in url - wordpress

I'm looking for a way to put a Woocommerce product ID in a URL, e.g.:
domain.com/product-category/id/product-title
The ID is the product's post ID, e.g. "12092". Automatically created by WooCommerce in Wordpress.
Can this be done in a way? Either easily through the Wordpress permalinks settings or in a different way through hacking my way into the files.
This nearly works:
<?php
add_filter('post_type_link', 'wpse33551_post_type_link', 1, 3);
function wpse33551_post_type_link( $link, $post = 0 ){
if ( $post->post_type == 'product' ){
return home_url( 'p/' . $post->ID );
} else {
return $link;
}
}
add_action( 'init', 'wpse33551_rewrites_init' );
function wpse33551_rewrites_init(){
add_rewrite_rule(
'product/([0-9]+)?$',
'index.php?post_type=product&p=$matches[1]',
'top' );
}
?>
But the output is:
domain.com/p/45
I'd like it to be:
domain.com/p/45/title-of-the-post
Thanks!

Why so complicated? Go to settings -> permalinks and click "Shop based URL" and save. Now you can see that the permalink structure in the "Custom structure". It should looks like this now:
/shop/%product_cat%/
Now add the post_id to the permalink setting like this:
/shop/%product_cat%/%post_id%/
Save it and go ahead.

You can use $post->post_name to get the slug.
Use a more permissive regular expression to accept any url that ends with /p/[id][anything]
<?php
add_filter('post_type_link', 'wpse33551_post_type_link', 1, 3);
function wpse33551_post_type_link( $link, $post = 0 ){
if ( $post->post_type != 'product' )
return $link;
return home_url( 'p/' . $post->ID . '/' . $post->post_name );
}
add_action( 'init', 'wpse33551_rewrites_init' );
function wpse33551_rewrites_init(){
add_rewrite_rule(
'p/([0-9]+)?.*?$',
'index.php?post_type=product&p=$matches[1]',
'top'
);
}
?>
You might need to 'reset' your permalinks in the settings menu for WordPress to accept the new rule.

Related

Convert GET variables from url to path using add_rewrite_rule in Wordpress

In my wordpress website (nginx hosting) I have a product page with this url:
https://www.officeshop.co.il/product/my-product/
I have developed some functionality that allows to view different product models using get parameter:
https://www.officeshop.co.il/product/my-product/?build=product-572
I want to rewrite this url to make it SEO friendly to look like:
https://www.officeshop.co.il/product/my-product/build/product-572
I understand that the function add_rewrite_rule will do the job, but what parameters should I pass to it in my case?
You can use the below code to your theme's function.php make your link SEO friendly.
add_filter( 'post_type_link', 'change_product_links', 10, 2 );
function change_product_links( $link, $post) {
if ( $post->post_type == 'tribe_events' && tribe_event_in_category('product') ) {
$link = trailingslashit( home_url('/product/' . $post->post_name ) );
}
return $link;
}
add_action( 'init', 'product_rewrite_rule', 5);
function product_rewrite_rule() {
add_rewrite_rule( '^product/([^/]+)/?', 'index.php?tribe_events=$matches[1]&post_type=tribe_events&name=$matches[1]', 'top' );
}

Replace wordpress slug title with metabox value in permalink

I have a custom post type for minutes of the meetings without a title. I wish that the permalink was something like www.website.com/minutes/2018-10-16/ in witch minutes is the registered custom post type name and 2018-10-16 is the metabox whit the meeting date (that I already created).
Ho can I do this?
Thanks!
I found the solution by following this tutorial.
This is the code posted by Milo for a custom post type called "product":
add_filter('post_type_link', 'wpse33551_post_type_link', 1, 3);
function wpse33551_post_type_link( $link, $post = 0 ){
if ( $post->post_type == 'product' ){
return home_url( 'product/' . $post->ID );
} else {
return $link;
}
}
add_action( 'init', 'wpse33551_rewrites_init' );
function wpse33551_rewrites_init(){
add_rewrite_rule(
'product/([0-9]+)?$',
'index.php?post_type=product&p=$matches[1]',
'top' );
}

WordPress Post Meta instead of title in Custom Post Type Permalink

Is it possible to use a post meta value instead of the title as permalink?
I tried different methods, but i did'nt work as i would like
As for now the structure is: www.website../property/post-title
i achived this up to now.. www.website../property/post-meta/post-title
what i would like is: www.website../property/post-meta
my code is:
function property_query_vars( $qv ) {
$qv[] = 'meta';
return $qv;
}
add_filter( 'query_vars', 'property_query_vars' );
add_filter( 'post_type_link', 'property_post_type_link2', 10, 3);
function property_post_type_link2($permalink, $post, $leavename) {
if ($post->post_type == 'property') {
$meta = get_post_meta($post->ID, '_boP_prop-id', true);
$postname = $post->post_name;
if (isset($meta) && !empty($meta))
$permalink = home_url( " property/" . $meta . "/");
}
return $permalink;
}
function mycustomname_rewrites_init2(){
add_rewrite_rule('property/([^/]*)/?$',
'index.php?post_type=property&meta=$matches[1]',
'top'
);
}
add_action('init', 'mycustomname_rewrites_init2');
this shows the permalink as i wanted but, only the archive page is shown, click on the permalink returns me to archive page.

Rewrite rules for EDD product page

I want my url changes depending on the term of taxonomy category_download custom post types download.
Example:
I have 2 categories:
– plugins
– themes
I wish I had a url for my single download:
/plugins/name_of_download/
AND
/themes/name_of_download/
Try adding this code to your themes functions.php file,
function __custom_messagetypes_link( $link, $term, $taxonomy )
{
if ( $taxonomy == 'plugins' ){
return str_replace( 'plugins/', '', $link );
}
else if ( $taxonomy == 'themes' ){
return str_replace( 'themes/', '', $link );
}
else{
return $link;
}
}
add_filter( 'term_link', '__custom_messagetypes_link', 10, 3 );
I am assuming the used slug, May be you have to change the your taxonomy slug if these are different.
If have any problem you can freely ask, Thanks,

Changing permalink to custom post type

I am trying to get my permalink to be something like this:
http://example.com/projects/1234
By default it is looking like this:
http://example.com/projects/title
I tried setting the "slug" to "projects/%post_id%" but then it looked like this:
http://example.com/projects/%post_id%/title
Is there a way to set the slug to my custom slug "/" the id for the post? Any help is appreciated.
A gift from me to you: Just replace "property" with the name of your custom post type. Goes in your theme's functions.php
add_filter('post_type_link', 'property_post_type_link', 1, 3);
function property_post_type_link( $link, $post = 0 ){
if ( $post->post_type == 'property' ){
return home_url( 'property/' . $post->ID );
} else {
return $link;
}
}
add_action( 'init', 'property_rewrites_init' );
function property_rewrites_init(){
add_rewrite_rule(
'property/([0-9]+)?$',
'index.php?post_type=property&p=$matches[1]',
'top' );
}
Is 'projects' a category or tag etc?
Something like the below should work if projects is a category or tag.
/%category%/%post_id%/

Resources