WordPress Custom Taxonomies - wordpress

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.

Related

Custom post with custom URL

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.

How to change URL of item in Wordpress dashboard in order to go directly to first post inside it

I created some custom fields for the Wordpress dashboard, for example:
functions.php
function create_field() {
register_post_type('field',
array(
'labels' => array(
'name' => 'Field',
'singular_name' => 'Field'
),
'public' => true,
'has_archive' => false,
'rewrite' => false,
'show_in_menu' => false,
'capability_type' => 'post',
'capabilities' => array(
'create_posts' => false
),
'map_meta_cap' => true,
)
);
}
add_action('init', 'create_field');
In some of them, only one post will be created. It will be continually edited and never deleted. So, what I want is to change de URL of this item in dashboard menu and go directly to this post inside it instead of showing the list of posts, because it will be no necessary.
For example, in the dashboard menu, when click in Products, instead of going to www.mywebsite.com/wp-admin/edit.php?post_type=products, I want it to go directly to the only post created inside it: www.mywebsite.com/wp-admin/post.php?post=24&action=edit.
Is that possible? I searched and tried a lot of things, since rewrite, different add_action functions, removing the item and trying to replace it and so on. I don't know exactly what else I can try for now.
I searched a little more without success. Unfortunately I had to use JavaScript, something I was avoiding, but solves it temporarily.
First, I used admin_enqueue_scripts action hook in functions.php to add JavaScript on the administration screens.
function admin_scripts() {
wp_enqueue_script( "", get_template_directory_uri() . "/scripts_admin.js", "", "", true );
}
add_action( "admin_enqueue_scripts", "admin_scripts" );
Then on my JavaScript I replaced the link hrefs:
// examples
document.querySelector("#menu-posts-products a").href = "post.php?post=10&action=edit";
document.querySelector("#menu-posts-contact a").href = "post.php?post=20&action=edit";
I still think there is a way to do it through Wordpress, maybe in the creation of Post Types. But for now, that solves it.

wordpress permalinks for custom taxonomy

I'm using tribe events in wordpress and been struggling with the following problem:
I have events (custom post type) filtered by category (default taxonomy for this plugin).
I'm using "pretty permalinks settings" so my url looks like that:
[website.com]/events/category/Amsterdam/ (when my category is Amsterdam).
And now to the question:
I sub filtered my posts by another custom taxonomy called “event_type” (concert, pub …)
when I'm trying to get all the posts from Amsterdam that are a "concert" type, my url is:
[website.com]/events/category/amsterdam/?event_type=concert
on default permalink settings the url is:
[website.com]/index.php?tribe_events_cat=amsterdam&event_type=concert
How do I get WordPress to transform and to recognize my url as:
[website.com]/events/category/amsterdam/event_type/concert/
I know it has something to do with the rewrite_rules_array but could not manage to do so.
Any help would be very appreciated!
Thanx in advance.
While You register custom post type:
function people_init() {
// create a new taxonomy
register_taxonomy(
'people',
'post',
array(
'label' => __( 'People' ),
'rewrite' => array( 'slug' => 'person' ),
'capabilities' => array(
'assign_terms' => 'edit_guides',
'edit_terms' => 'publish_guides'
)
)
);
}
add_action( 'init', 'people_init' );
checkout the "rewrite" parameter. More You can check here:
http://codex.wordpress.org/Function_Reference/register_taxonomy
Also sometimes You need to flush permalink structure by going to permalink settings and clicking save (dont ask me why) ;).

Wordpress - adding custom post type category to menu

I have a custom post type named 'The Books', and a relative category named 'The Books' for these custom posts.
When I add the category The Posts to my nav menu, it doesn't work because it goes to the URL /category/the-books instead of just going to /the-books. If I posted this in the default post section it shows correctly, but when I post in the custom post section it does not return my post. I can, of course, add individual posts from my custom post section to the nav menu, but can't figure out how to add an archive page of the custom posts.
My permalinks are set to: URL/%postname%/ so I'm not sure why that is happening.
Here's the function for my custom posts:
add_action( 'init', 'create_post_type' );
function create_post_type() {
register_post_type( 'hpl_books',
array(
'labels' => array(
'name' => __( 'The Books' ),
'singular_name' => __( 'Book' )
),
'taxonomies' => array('category'),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'the-books'),
)
);
}
Any advice is greatly appreciated.
thanks!
You shouldn't need to add a category "the-books" in order to display the results.
Have you created a view in your page-templates directory called "archive-hpl_books.php?" That's the file WordPress will look for to display the archive of your custom post type. Basically, you would create a page called "the-books" or whatever, then set archive-hpl_books.php as the template.
See http://codex.wordpress.org/Template_Hierarchy
I'm only responding to this because I just went through a similar issue, so I'm down to help out. :)

wordpress create custom post type that pulls custom post types

I'm customizing the admin on a new WP site, and I am creating an over-arching custom post type, that will allow the admin to create pages for the site. I want them to be able to pull values from other custom post types and set them here. I'm struggling to find good documentation on how to do this though. I can create multiple custom post types without a problem, just unsure how to pull the values of post_type_y into the meta_options for post_type_x.
Any and all help is appreciated!
Since I can't 'comment' on your question, I'll do my best to answer it as I understand it..
There's a plugin posts to posts that will allow you to associate Post 1 with Post 2. It's a little clunky, but gets the job done.
If you're looking to associate tags, categories or whatever between multiple post-types, I prefer using custom taxonomies as they are relatively easy to implement.
Sample Custom Taxonomy:
function languages_init() {
// create a new taxonomy
register_taxonomy(
'languages',
array('post','clients','positions','projects'), // Set various post-types
array(
'label' => __( 'Languages' ),
'sort' => true,
'args' => array( 'orderby' => 'term_order' ),
'rewrite' => array( 'slug' => 'language' )
)
);
}
add_action( 'init', 'languages_init' );

Resources