• Resolved Regan Khadgi

    (@regankhadgi)


    Hello,

    I am trying to add separate metabox to assign the primary category whenever users selects multiple category. But I am not being able to get the category ID whenever user checks or unchecks the categories? All the category checkbox contains the value as 1. I am not sure how WordPress is getting the category ID from checked categories.

    Reference screenshot:
    https://prnt.sc/7FxrIvvswuu_

    Any help will be highly appreciated.

    Thanks.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    The “1” value is actually treated as true, indicating the box was checked. It seems all you have to work with in the HTML is the checkbox’s ID attribute and the corresponding label’s “for” attribute, which would give you the category term’s name.

    How is the term ID obtained when the checked terms are sent to the server? Sorry, I don’t know. I imagine the script that manages this some how correlates the control’s hash value with the term ID within its own code. I fear it’d be difficult to get this correlation from the script. You might be forced to query for the term object based on name. This isn’t so simple since child terms can have the same name since their ancestry is different. To accurately get a term object by name you need to establish its ancestry as well.

    Is the idea of this primary category meta box to allow users to select a primary among all the categories they’ve selected? What if you developed a meta box with checkboxes for assigning a category and radio buttons for primary? Then hide the default meta box, leaving your custom meta box as the only place to manage categories?

    Thread Starter Regan Khadgi

    (@regankhadgi)

    Hi @bcworkz,

    Thank you for your inputs on this and giving me the idea. Since this is for a custom site which doesn’t have the multiple categories with same name so I have decided to not store the ID but fetch the category name and save it as the primary category which should do the job I guess ??

    Have a nice day!!
    Cheers!!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Get the category id whenever user checks any category in Post Edit Screen’ is closed to new replies.