Custom post with custom URL - wordpress

I have created a custom post, and I would like the URL to be:
example.com/directory/profile/slug/
With that in mind, this is a snap shot of how I have set it up:
$args = array(
...
'rewrite' => array( 'slug' => 'directory/profile' ),
'supports' => array( 'title', 'thumbnail' ),
);
I have created a corresponding file:
single-profile.php
If I remove the directory/ from the rewrite parameter, the new post shows up at this URL:
example.com/profile/slug/
I have looked at adding rewrite rules but either thats not the right approach or I am not doing that correctly.
Example:
add_rewrite_rule(
'^directory/profile/([^/]+)/?$',
'index.php?pagename=$matches[1]&post_type=profile',
'top'
);
What extra steps do I need to get this to work as intended?

An easy way to do it is: go to settings > permalinks, select Custom and set the value to /%postname%/.
No need for the rewrite rules.
Keep the post type slug as directory/profile.
Note that this will also change the default posts URLs to /postname. If you want to apply this change only for the post type, you can use this plugin, making the change above only for this post type.
Or do it on the code using the post_type_link hook, also works.

Related

Defining page layout for defualt post formats (aside)

single-aside.php defined but still using single.php for my post that having aside format.
Hi i used post-formats(aside) for some of my post and using single.php for styling them in the past despite defining a new post-type , the problem is now i want to use another page layout diffrent than my other posts and pages just for this aside type , but when i create single-aside.php wordpress doesnt use this page , and i flushed away my permalinks thats not the problem. do you think single page is defineable for defualt post formats like aside, video,quote and... ? any idea?
As per documentation you can use has_post_format() see more here https://codex.wordpress.org/Post_Formats under Using Formats.
Since the post formats aren't custom post types, you would need to add it to single.php:
if ( has_post_format( 'aside' )) {
//Your custom code for aside goes here
}
Alternatively, if you would like to use a different php file, you can create your custom post type and give support for Post formats.
Register with support:
add_action( 'init', 'aside_post_type' );
function aside_post_type() {
register_post_type( 'aside',
array(
'labels' => array( 'name' => __( 'Aside' ) ),
'public' => true,
'supports' => array('title', 'editor', 'post-formats') //this will give support for post formats
)
);
}
Then add single-aside.php to your theme and style this custom post type as needed.

Change custom post type url/slug in wordpress?

i want to change my custom post url/slug from "property" to "project".
From this https://ayanaproperties.com/property/chelsea-creek-development-sw6-london/
To this https://ayanaproperties.com/project/chelsea-creek-development-sw6-london/
what method/plugin or any function to use?
You should search your theme for register_post_type function.
When you find it, make sure that it register your post type, and search for rewrite param. If it doesn't exists add it like the example below:
'rewrite' => array(
'slug' => 'developer',
),
Then make sure that you have updated your permalink structure.

WordPress | Modify rewrite rule

I have start maintaining a web site, that it is build from another person, and I like to modify the rewrite rules applied for a custom post type.
more specific, the custom post type is buidled with the following settings :
$args = array(
...
'rewrite' => array(
'slug' => 'myposttype',
'with_front' => false
),
....
)
register_post_type('myposttype', $args);
The permalink structure is the following:
Post name http://www.mysite.gr/sample-post/
but the url structure is the following:
http://www.mysite.gr/myposttype/my_post_slug/
how can I change it to
http://www.mysite.gr/my_post_slug/
Any idea please ?
Note: This is not a good idea as it may conflict with pages having the same slug.
Here is a tutorial on how to remove the post type name from the url.

Using Dates in Custom post_type Permalinks in Wordpress 3.0

I'm adding a custom post_type to Wordpress, and would like the permalink structure to look like this:
/%post_type%/%year%/%monthnum%/%postname%/
I can't figure out how to add the date tags. Using this code, gives me /my_type/example-post-slug/:
register_post_type( 'customtype', array(
...other options...
'rewrite' => array('slug' => 'my_type'),
));
How do I include the dates?
You can achieve this with the plugin Custom Post Type Permalinks. Just install the plugin and change the permalink format in the settings.
You need to add WordPress structure tags to your rewrite attribute like so:
register_post_type('customtype',array(
....
'rewrite' => array('slug' => 'customtype/%year%/%monthnum%','with_front' => false)
));
Then add a post_type_link filter to rewrite the structure tags in your URLs for the custom post, so that the tags work:
function custompost_post_type_link($url, $post) {
if ( 'customtype' == get_post_type($post) ) {
$url = str_replace( "%year%", get_the_date('Y'), $url );
$url = str_replace( "%monthnum%", get_the_date('m'), $url );
}
return $url;
}
add_filter('post_type_link', 'custompost_post_type_link', 10, 2);
You can refer to this article for copypasta code (encapsulated in a class though) on creating custom posts like this. The article has a few extra bits of explanation and a few pieces of additional functionality too: https://blog.terresquall.com/2021/03/making-date-based-permalinks-for-custom-posts-in-wordpress/
EDIT: By the way, you'll also need to flush your permalinks after you're done for this to work.
I have found a partial solution which allows for the permalink to be recognized and preserved upon loading the page in the address bar, but not updated in the edit screen or other links to the post on the site.
Add the following to functions.php or a site specific plugin, replacing example-post-type with the identifier of your post type.
function example_rewrite() {
add_rewrite_rule('^example-post-type/([0-9]{4})/([0-9]{1,2})/([^/]*)/?','index.php?post_type=example-post-type&year=$matches[1]&monthnum=$matches[2]&name=$matches[3]','top');
}
add_action('init', 'example_rewrite');
This uses the Rewrite API documented here
To find more tips on understanding the process see here.
One thing to bear in mind is no matter how you do this, it is impossible for two posts to have the same slug, even if they have different dates. This is because if the permalink scheme is ever changed, they could clash and cause errors.
Use this it's working 100% :
'rewrite' => array('slug'=>date('Y').'/'.date('m').'/custom_post_type_slug','with_front'=>true)

WordPress Custom Taxonomies

I have the following code to build a custom taxonomy for my portfolio:
add_action( 'init', 'create_pc_db_taxonomies', 0 );
function create_pc_db_taxonomies() {
register_taxonomy( 'type', 'post', array( 'hierarchical' => true, 'label' => 'Type', 'query_var' => true, 'rewrite' => array( 'slug' => 'type' ) ) );
}
I have created a portfolio category on my site (I removed the /category/ base) and have created some items and set the custom taxonomies against them. So I get the following setup:
http://domain.com/portfolio/item1/ but what I want is for the taxonomy links to look like this: http://domain.com/portfolio/type/web and then this will show a list of portfolio items related to the that type. At the moment they are root like http://domain.com/type/web these create 404's, but I have also tried adding 'portfolio/type' as the taxonomy slug but it just creates a 404 as well, but i'm pretty sure this is the wrong way of doing it anyways. Any help?
Thanks
EDIT: The site is here: http://driz.co.uk/ all the work is in a category called Portfolio and under each pic is the title and custom taxonomy (if you click on them you will get the 404)
Make sure all plugins are disabled (at least ones that tweak with rewrites), and delete the option rewrite_rules in your wp_options table.
Now remove any hooks in your theme that meddle with custom taxes and post types, and run this on init;
register_taxonomy(
'type',
'post',
array(
'hierarchical' => true,
'label' => 'Type',
'rewrite' => array('slug' => 'portfolio/type')
)
);
In your permalink settings, choose one of the default options, and make sure both a category and tag base is set, then update.
Dump $wp_rewrite as you did before, and you should see a few lines like so;
[portfolio/type/([^/]+)/page/?([0-9]{1,})/?$] => index.php?type=$matches[1]&paged=$matches[2]
[portfolio/type/([^/]+)/?$] => index.php?type=$matches[1]
These should be near the very beginning of the array, before tags, categories and the like.
Let me know how you get on!
(When I run the code in my install of WP 3.0-beta-2 it works, so I assume something funky is happening at the moment).
I'm sure you can fake a slug like that, using portfolio/type - the key is, once you've updated your code, you'll need to flush the rewrite rules.
Do this by simply re-saving your permalink settings in the admin.

Resources