Replace wordpress slug title with metabox value in permalink - wordpress

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' );
}

Related

Display CPT list page (archive) with category term on slug

I have the CPT (Custom Post Type) "news".
In the "category" taxonomy I have "food" and "health".
The archive page is accessed via the url:
www.mysite.com/news
Is there any way to show "news" by categories? Ex:
www.mysite.com/food/news
www.mysite.com/health/news
Thank you all in advance.
In that link I found the following solution:
The solution for me had three parts. In my case the post type is called trainings.
Add 'rewrite' => array('slug' => 'trainings/%cat%') to the
register_post_type function.
Change the slug to have a dynamic
category. "Listen" to the new dynamic URL and load the appropriate template.
So here is how to change the permalink dynamically for a given post type. Add to functions.php:
function vx_soon_training_post_link( $post_link, $id = 0 ) {
$post = get_post( $id );
if ( is_object( $post ) ) {
$terms = wp_get_object_terms( $post->ID, 'training_cat' );
if ( $terms ) {
return str_replace( '%cat%', $terms[0]->slug, $post_link );
}
}
return $post_link;
}
add_filter( 'post_type_link', 'vx_soon_training_post_link', 1, 3 );
...and this is how to load the appropriate template on the new dynamic URL. Add to functions.php:
function archive_rewrite_rules() {
add_rewrite_rule(
'^training/(.*)/(.*)/?$',
'index.php?post_type=trainings&name=$matches[2]',
'top'
);
//flush_rewrite_rules(); // use only once
}
add_action( 'init', 'archive_rewrite_rules' );
Thats it! Remember to refresh the permalinks by saving the permalinks again in de backend. Or use the flush_rewrite_rules() function.
But I have a question that I couldn't do there:
This part:
Add 'rewrite' => array('slug' => 'trainings/%cat%') to the register_post_type function.
where do I do that? Or is this already embedded in the later code?

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,

Woocommerce product ID in url

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.

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