Custom Post Types with Parent Pages - best practice - wordpress

I am working on a site with product categories and individual products organized like this:
product category 1
product
product
product
product category 2
product
etc...
It was suggested to me that the best way to do organize the individual products was to create a custom post type for products. The product categories are currently pages.
So now I am trying to figure out the best way to connect a product custom post type with its particular product category page (so that url structure can be "/product_category/product/").
I am sure there is a smart and efficient way to do this, but I'm not seeing it. I was thinking that I could add categories to the pages and CPT's - but that isn't quite the effect that I want because I don't want them to only be accessible as archive pages. Is that actually a problem?
Sorry that this is such a broad question - I can get it to work but am sure that my first try would not be the best way, so I am just looking for some pointers or examples from people who have done this before to steer me in the right direction.
Thanks in advance, any help appreciated.

You can use Custom Taxonomies.
function product_categories_init() {
register_taxonomy(
'product-category',
'product', // Or 'post', whatever the custom post type is
array(
'label' => __( 'Product Categories' ),
'rewrite' => array( 'slug' => 'product-categories' ),
'hierarchical' => false,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
)
);
}
add_action( 'init', 'product_categories_init' );
This is a simple example. You'll have to check out the options in the reference link. But it is essentially your own kind of category for a post type (or an array of post types).

In the end I did get this right. Part of my problem was due to a misunderstanding about how to use categories in wordpress.
What I ended up doing was use the custom taxonomy as shown by Austin Winstanely's answer, and then refactor my page organization a little bit so that the product category was not a page post type, but a category - displayed using the category template. This makes it very convenient, because individual products in the custom post type can be associated with their product category very easily. When I asked the question I had not understood that categories could have their own template and function as web pages.
Thanks for the help!
-Alyssa

Related

Any way to add metaboxes in custom settings page of custom post type in wordpress? [duplicate]

This question already has an answer here:
Add meta box to WordPress options page
(1 answer)
Closed 4 years ago.
Hi I would like to add metaboxes under custom settings page which is under a custom post type. I can create metaboxes for custom post types also I can create a theme options. But can't find any way to add the metaboxes on a custom settings page. Like my post type hierarchy is like below:
Products
- All item
- Add Item
- Product Category
- Product Settings
I want to add the metaboxes & create a options page on that settings page. Can you please guide me through this one.
I've been trying to follow this gist but can't find a way.
https://github.com/WebDevStudios/CMB2-Snippet-Library/blob/master/options-and-settings-pages/theme-options-cmb.php
Also can you let me know if I can achieve something by tweaking this code where key|value operates
$cmb = new_cmb2_box( array(
'id' => $this->metabox_id,
'hookup' => false,
'show_on' => array(
// These are important, don't remove
'key' => 'options-page',
'value' => array( $this->key, )
),
) );
I've created the settings page by this code
add_submenu_page('edit.php?post_type=ch_product_showcase', 'Product Showcase Settings', 'Showcase Settings', 'edit_posts', basename(__FILE__), array( $this, 'chProductShowcaseSettingsOptions') );
I've done it many times. Use this code and tweak it to your needs:
https://gist.github.com/turtlepod/5203512
Found as a link in the comments of this page:
https://gist.github.com/bueltge/757903
Originally posted here:
Wordpress - Add meta box to options page
Normally I don't like answering with links to another site, but in this case the code is on gist and hopefully will never go away!

wordpress custom archive pages

I'm developing a wordpress page and I'm looking for the best practices to send custom DB queries. I created a archive page with a list of artists. These names are stored in a custom post type's custom taxonomy. Now I want to code links on every name, which should lead the visitor to a page where all the posts that have this artists name in this custom field.
I know how to create the custom DB query, but how do I submit the name? Just over a normal POST-request? Is there a convenient way to do this within WP?
Thanks for your help.
Dan
you said in your question "custom post type's custom taxonomy"
you can create a custom taxonomy template to list all posts under a particular taxonomy. you can use this detailed tutorial how to use taxonomy.
http://code.tutsplus.com/tutorials/introducing-wordpress-3-custom-taxonomies--net-11658
I think the best way to do this would be store the artists name as a meta value attached to the post. Then you could use Wordpress inbuilt meta query to easily output all posts with the artists name stored as a meta value.
$meta_query_args = array(
array(
'key' => 'artist',
'value' => 'John Doe',
'compare' => '='
)
);
$meta_query = new WP_Meta_Query( $meta_query_args );

Custom Post Type Admin Post Status Not Working

For the life of me, I can not figure this out.
I set up a custom post type in WordPress like so:
register_post_type( 'myposttype',
array(
'labels' => array(
'name' => __( 'MyPostTypeName' ),
'singular_name' => __( 'MyPostTypeName' )
),
'public' => true,
'has_archive' => true,
'supports' => array('title', 'custom-fields')
)
);
When I first started I added add_filter( 'map_meta_cap', 'my_map_meta_cap', 10, 4 ); with some basic capabilities. I think I may have set that part up wrong, but I quickly got rid of it and returned the post type to normal capabilities.
Now whenever I try to access the custom post type in my admin, I run into some weird problems. Namely, no matter what Post status I click on (Published, Drafts, Private, etc.) I only get published posts. Also sorting the columns and other similar admin functions don't work at all. It's like edit.php is completely broken, but only for this specific custom post type. It works just fine on all other custom post types.
I've tried everything, right down to deleting every mention of the custom post type from the admin panel and then trying to register it again, and nothing works. I really need to keep the slug from the custom post type the same, but I'd be open to registering a new post type, moving posts over and then changing the slug I just can't figure out a good way to do it.
Anyway, I'd appreciate any help in figuring out how to fix the custom post type admin panel.
Thanks
Do you have a "posts_orderby" filter added anywhere?

Adding custom taxonomy to media gallery in wordpress admin

I have added a custom taxonomy to Media, which is showing up as a text field in the Media admin section. I would like this to be the typical checkbox format as it exists in the custom post type admin page. Is there a way to override this in the functions to make this custom taxonomy show in checkboxes, so the user could easily choose which image belongs to a specific taxonomy entry?
Here is the code I used to bring the taxonomy into the Media Gallery:
register_taxonomy('Categories',array('project', 'slides', 'attachment'), array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'categories' ),
));
In the first line, by adding 'attachment' to the array, it added the Project Categories field in the Media Gallery. Now I just need to make this a list of checkboxes containing the current taxonomy entries. Any thoughts on how to achieve this?
I found this article, but having never used filters, it was a bit perplexing as to how to make this work for me:
https://wordpress.stackexchange.com/questions/29858/adding-category-tag-taxonomy-support-to-images-media
You are most of the way there. To render a taxonomy category as a special HTML display, like a list of checkboxes, the best method is to use the built-in WordPress Walker class. It is made exactly for this sort of thing.
http://codex.wordpress.org/Function_Reference/Walker_Class
I use this very method to create a new "SLP_Tagalong" walker class that renders a list of my taxonomy categories as a list of checkboxes (I only show text names but it could easily show the marker images) whenever anyone edits a store location.
I have the modified Walker Class I can share if you would like to see it. I'd post here but it is 150 lines. Send me a PM and I'll shoot it back that way.
I am sure the walker class would have worked successfully, but looking at the codex reminded me of string theory and existentialism. The upside is with WP 3.5.1, when you associate a taxonomy to 'attachment' set to Hierarchal, the checkbox appears in the Media Library by default.
YAY!!
This may not answer the question posed thoroughly though so I will leave it open for anyone who wants to stab at this.

How do you remove a Category-style (hierarchical) taxonomy metabox

I was wondering if someone can help me with this. I'm currently following Shibashake's tutorial about creating custom meta-boxes that include taxonomy selection here: http://shibashake.com/wordpress-theme/wordpress-custom-taxonomy-input-panels .
They show how to remove the standard metabox Wordpress automatically creates for taxonomies using the remove_meta_box function. Only problem is that the function for some reason doesn't seem to work on taxonomies that work as categories ie ones where the hierarchical option is set to true. I know I have the function working because the ones set up as tags disappear easily enough. I can't if it just isn't possible or if there is something special I need to add in one of the parameters to make it work.
Example:
$args = array(
'hierarchical' => false,
'label' =>'People',
'query_var' => true,
'rewrite' => true
);
register_taxonomy('people', 'post',$args);
remove_meta_box('tagsdiv-people','post','side');
That works fine. If I set hierarchical to 'true, however, the meta box stays put.
Can anyone shed some light?
Found the answer asking over at the Wordpress side of StackExchange:
For taxonomies that work like tags, you use "tagsdiv-slug". But for ones that are hierarchical, you use "slugdiv". The answe can be found here:
Thanks to #Jan Fabry for his answer

Resources