0

I usually set a custom field customdata for posts and pages, that I can obtain when rendering a page with this PHP code:

get_post_meta(get_the_ID(), 'customdata', true)

Is there a way to set a custom field customdata to category mycat, so that, when rendering the category view:

http://example.com/category/mycat

I can have access to customdata too from PHP code?

6
  • You have a option to add, update and get term meta as well, check update_term_meta, get_term_meta, add_term_meta. The difference is when in category, the way to get the term id is get_queried_object_id() Commented Nov 2, 2021 at 8:54
  • Thank you @Buttered_Toast. I'd like my rendering code to work for a page, for a post, for a category, etc. Is there something that works for all types? Does get_post_meta(get_queried_object_id(), 'customdata', true) work for pages and posts as well?
    – Basj
    Commented Nov 2, 2021 at 9:18
  • Also @Buttered_Toast, I see no GUI textbox in the admin page "Edit category" to add a custom field. Can I do, once for all from PHP code in add_action('init', ..., with add_term_meta($categoryid, "customdata" , 'mydata');?
    – Basj
    Commented Nov 2, 2021 at 9:20
  • get_post_meta works for all post_types, so pages and posts will work with that. category is a taxonomy so get_post_meta will not work for it, you will need to create so if logic that will check if you are in a post or category and run the appropriate function. Commented Nov 2, 2021 at 9:21
  • If you would like to visually see the metas I would suggest using Advanced Custom Fields, this plugin will create meta boxes for you based on your needs, it have some built in function to call that data as well. Will save you alot of headache Commented Nov 2, 2021 at 9:24

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.