I have created a custom post type as well as a custom taxonomy, however when I added my first item and selected a category/recipient something weird happened, it automatically created an additional numeric category and changed the selection to that.

eg;

enter image description here

The only recipients I had entered were "John" and "Michael", I selected John and when the page reloaded it had added "13" and had it selected, and every time I update it it adds a new number and selects this new number as the recipient.

It seems the only time it doesn't add a new number is when I don't select any recipient.

The code:

add_action( 'after_setup_theme', 'blm_theme_setup' );

function blm_theme_setup() {

    //.........

    // Add our custom post type
    add_action( 'init', 'blm_letter_post_type' );

   //........

}

function blm_letter_post_type() {

    register_post_type( 'letter',
                        array(
                            'labels'     => array(
                                'name'          => __( 'Letters' ),
                                'singular_name' => __( 'Letter' ),
                                //..........
                            ),
                            'public'     => true,
                            'taxonomies' => array( 'recipient' ),
                            'supports' => array('title', 'editor', 'revisions'),
                            'has_archive' => true,
                            'rewrite' => array('slug' => 'letters'),
                            //........
                        )
    );

    // Our args for the custom taxonomy below
    $args = array(
        'labels'      => array(
            'name'          => __( 'Recipients' ),
            'singular_name' => __( 'Recipient' ),
            //.....
        ),
        'meta_box_cb' => 'post_categories_meta_box',
    );

    // Register a custom taxonomy for our letter categories
    register_taxonomy( 'recipient', 'letter', $args );

    // Connect the post type and taxonomy together to be safe
    register_taxonomy_for_object_type( 'recipient', 'letter' );

}

Note: I tried to only include relevant information to reduce fluff, any non-included arguments were either labels or irrelevant arguments such as description, menu position and menu icon.

Edit: I have just noticed that the "category" it creates matches the term id of the "category" I selected. So if the term id of John is say 24 and I select that, then it will create a new "category" with that number and select it.

What is going on here and how do I fix it?

  • Can you uncensor the missing parts of your argument arrays? Also, the number, is it the term ID of the term you chose? Why have you defined meta_box_cb? – Tom J Nowell Aug 12 at 17:07
  • @TomJNowell Do you mean the argument array for the taxonomy? If so, the only thing I have taken out are other labels and the description. As for the term id I am unsure, this is my first time working with custom taxonomies; I can update the question with related information from the DB if that helps. I defined meta_box_cb because I need a non-hierarchical post type that acts like a category selector and without it I get tag functionality. – Brett Aug 12 at 17:16
  • hmmm that could be problematic, but including the other info verifies to everybody else that is indeed, only a description etc. People have a habit of thinking important things aren't relevant when they are, and omitting them, it's better to provide too much information than too little – Tom J Nowell Aug 12 at 20:11
  • Also, using that metabox callback is no guarantee that it will work as you expect it to. Perhaps this is an XY question, where Y is the question above, and X is the original problem of how to show non-hierarchical taxonomy terms as checkboxes instead of tags – Tom J Nowell Aug 12 at 20:12
  • That's true - though you would think there would be some kind of warning if doing something like that could cause potential issues. – Brett Aug 12 at 20:55
up vote 2 down vote accepted

This is a bug in wordpress core when there is a mismatch between the type of taxonomy the taxonomy and the metabox type you use to display it. you must use hierarchical display for hierarchical taxonomy, and "tag like" display for non hierarchical taxonomies. If you have to have a mix you will need to write your own metabox. This bug is very old, and it doesn't seem like it will be solved any time soon.

  • @Brett that doesn't mean you can't provide your own callback function – Tom J Nowell Aug 13 at 16:41
  • @TomJNowell That's true :) – Brett Aug 13 at 16:56

You are using incorrect syntax - try like this:

add_action( 'after_setup_theme', 'blm_theme_setup' );

function blm_theme_setup() {

    // Add our custom post type
    add_action( 'init', 'blm_letter_post_type' );

}

function blm_letter_post_type() {

register_post_type( 'letter',
                    array(
                        'labels'     => array(
                            'name'          => __( 'Letters' ),
                            'singular_name' => __( 'Letter' ),
                            //..........
                        ),
                        'public'     => true,
                        'taxonomies' => array( 'letter' ),
                        //........
                    )
);

// Our args for the custom taxonomy below
$args = array(
    'labels'      => array(
        'name'          => __( 'Recipients' ),
        'singular_name' => __( 'Recipient' ),
        //.....
    ),
    'meta_box_cb' => 'post_categories_meta_box',
);

register_taxonomy('recipient', 'letter',  $args);

}
  • What did you change? Only difference I can find is you removed register_taxonomy_for_object_type which the codex advises to use - check the last paragraph in the usage section. – Brett Aug 12 at 18:53
  • @Brett you should use register_taxonomy_for_object_type for already registered taxonomy, just use normal way and it will work - register_taxonomy – Alex P Aug 12 at 19:00
  • Ok I tried it, deleted all the current terms, re-added the ones I wanted and then set a recipient for the letter and it still created a new numerical recipient. – Brett Aug 12 at 20:46
  • The code I pasted is from fresh website copy, code is functional and works properly. make sure your code in functions.php is correct - seems to be one more function that affects it. – Alex P Aug 12 at 20:48
  • In your first reply you have: // Register a custom taxonomy for our letter categories register_taxonomy( 'recipient', 'letter', $args ); // Connect the post type and taxonomy together to be safe register_taxonomy_for_object_type( 'recipient', 'letter' ); //REMOVE THIS LINE – Alex P Aug 12 at 21:15

The problem comes from this line in the custom post type definition :

                        'taxonomies' => array( 'letter' ),

I don't have searched in code where is the bug but it looks like that creating a taxonomy with the same slug that the custom post type creates the kind of bug.

  • Thanks for noticing that; they actually don't have the same name, I just input the incorrect name in there; fixed it, but it made no change, still the same problem. – Brett Aug 13 at 11:43

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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