1

I have created a custom post type and a custom taxonomy. The issue is that I can not make the custom posts hierarchical with the categories it is in. I have the following code,

function AppManager_custom_post() {
  $labels = array(
    'name'               => _x( 'APKS', 'post type general name' ),
    'singular_name'      => _x( 'APK', 'post type singular name' ),
    'add_new'            => _x( 'Add New APK', 'APK' ),
    'add_new_item'       => __( 'Add New APK' ),
    'edit_item'          => __( 'Edit APK' ),
    'new_item'           => __( 'New APK' ),
    'all_items'          => __( 'All APKs' ),
    'view_item'          => __( 'View APK' ),
    'search_items'       => __( 'Search APKs' ),
    'not_found'          => __( 'No APK found' ),
    'not_found_in_trash' => __( 'No APKs found in the Trash' ),
  );
  $args = array(
    'labels'        => $labels,
    'description'   => 'Holds Apps and meta data',
    'public'        => true,
    'menu_position' => 5,
    'supports'      => array( 'title', 'thumbnail', 'excerpt' ),
    'has_archive'   => true,
      'hierarchical'  => true,
    'rewrite'       => array('slug' => 'APKs')
  );
  register_post_type( 'apps_post', $args );
  flush_rewrite_rules();
}
add_action( 'init', 'AppManager_custom_post' );

And I have the following to create the taxonomy,

function AppManager_custom_taxonomy() {
  $labels = array(
    'name'              => _x( 'App Categories', 'taxonomy general name' ),
    'singular_name'     => _x( 'App Category', 'taxonomy singular name' ),
    'search_items'      => __( 'Search App Categories' ),
    'all_items'         => __( 'All App Categories' ),
    'parent_item'       => __( 'Parent App Category' ),
    'parent_item_colon' => __( 'Parent App Category:' ),
    'edit_item'         => __( 'Edit App Category' ),
    'update_item'       => __( 'Update App Category' ),
    'add_new_item'      => __( 'Add New App Category' ),
    'new_item_name'     => __( 'New App Category' ),
    'menu_name'         => __( 'App Categories' ),
  );
  $args = array(
    'labels'       => $labels,
    'hierarchical' => true,
    'rewrite'      => array( 'hierarchical' => true, 'slug' => 'Apps' )
  );
  register_taxonomy( 'appCategory', 'apps_post', $args );

}
add_action( 'init', 'AppManager_custom_taxonomy', 0 );

No matter how I try the hierarchical function it never maps to the category that I add it to. Is it possible?

  • Setting Hierarchical to true on the Post Type allows you to set parents to posts, setting Hierarchical to Taxonomies allows you to set parents to terms. What are you trying to do exactly? What is your expected output? – Howdy_McGee Aug 23 '14 at 3:10
  • I want the URLs to be example.com/custom-taxonomy-parent/child1/custom-post and I also want them "linked" for lack of a better word so that I can populate posts by categories and such – Grady D Aug 23 '14 at 3:11
  • What if there's multiple children are you wanting to keep stacking them in the URL? I don't have the code on hand but I believe it's a fairly common question if you search around this site. – Howdy_McGee Aug 23 '14 at 3:15
  • Yep I want to keep stacking on children to the URL lol. I will try to search around and see if anything comes up. – Grady D Aug 23 '14 at 3:20
  • @Howdy_McGee I was unable to find anything, maybe I don't know the right terms to get what I am trying to do. I will make a note that they are 'linked' if I am on example.com/cat1 and post-example is a member of cat1 I can manually adjust the url to be exmaple.com/cat1/post-example and that works but it redirects me to example.com/apks/post-example instead of keeping the cat1. Can you point me in the right direction? – Grady D Aug 23 '14 at 17:07
1

This might not answer your question, but you have a few issues with your code

  • Do not use uppercase letters in function names, taxonomy names and custom post type names. Only use lowercase letters. Separate names with a underscore (_). Also, just a tip, never start these names with letters, or use a hyphen (-) to separate names

  • Never use flush_rewrite_rules(); in the fashion you have done. It is an extremely expensive operation to use. As it stands, it will load on every page load. This will increase your loading time considerable, which is bad when it comes to SEO. Check the codex for proper use.

  • Do not create multiple functions which is hooked to the same hook. Create one function, and hook that function to the desired hook

  • Thanks for the tips! I have put everything to lowercase instead of camel case and this did fix an issue I was having! How do you recommend I flush the rewrite rules when the plugin is activated but not on every page? – Grady D Aug 23 '14 at 16:00
  • 1
    You can have a look at this post. – Pieter Goosen Aug 23 '14 at 17:12
  • 1
    Did not solve my issue but still very good information thank you! – Grady D Aug 23 '14 at 17:34
0

Make the following modifications, I hope it will do the job

function AppManager_custom_taxonomy() {
  $labels = array(
    'name'              => _x( 'App Categories', 'taxonomy general name' ),
    'singular_name'     => _x( 'App Category', 'taxonomy singular name' ),
    'search_items'      => __( 'Search App Categories' ),
    'all_items'         => __( 'All App Categories' ),
    'parent_item'       => __( 'Parent App Category' ),
    'parent_item_colon' => __( 'Parent App Category:' ),
    'edit_item'         => __( 'Edit App Category' ),
    'update_item'       => __( 'Update App Category' ),
    'add_new_item'      => __( 'Add New App Category' ),
    'new_item_name'     => __( 'New App Category' ),
    'menu_name'         => __( 'App Categories' ),
  );
  $args = array(
    'label'        => $labels,
    'rewrite'      => array( 'slug' => 'Apps' ),
    'hierarchical' => true
  );
  register_taxonomy( 'appCategory', 'apps_post', $args );

}
add_action( 'init', 'AppManager_custom_taxonomy', 0 );
  • Sadly, this did not work. I modified your 'label' to 'labels' as it was throwing an error. I also changed my permalink structure within wordpress to be "Custom Structure" and having an input of /%category%/%postname%/ but the structure of the URL is still example.com/APKs/post-name instead of being example.com/cat1/child1/post-name. Is this a different issue or are they related? – Grady D Aug 23 '14 at 16:59
  • You might be having this problem when you're selecting multiple categories for a single post.. this problem has already been addressed in codex.wordpress.org/Using_Permalinks WP Category Permalink wordpress.org/plugins/wp-category-permalink The mentioned plugin allows manual selection of a 'main' category for each post for better permalinks and SEO. – Hassan Alvi Aug 24 '14 at 18:39
  • Sadly this plugin does not work with custom taxonomies. It only works with OOTB wordpress posts/pages. – Grady D Aug 24 '14 at 19:16
  • if you're interested in using taxonomies in wordpress permalinks, you can refer to this solution .. shibashake.com/wordpress-theme/… also, give this plugin a try wordpress.org/plugins/custom-post-type-permalinks – Hassan Alvi Aug 24 '14 at 19:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.