0

I am new to wordpress but I was trying to add custom fields to certain posts but I don't want to manually add every time. I saw there is a custom post type class that you can specify but I didn't find any related info when I searched. Essentially I want to add metadata so that I can add search functionality on posts. ie. rating, difficulty, etc.

  • Usually after adding a custom field once, there's a dropdown on a page or post so you can choose it easier and just provide a value. Are these the same fields everywhere with the same values or are they different values? Also try looking into ACF if you're looking for the plugin route: advancedcustomfields.com – RachieVee Oct 2 '14 at 19:50
  • Also might wanna try add_post_meta: codex.wordpress.org/Function_Reference/add_post_meta – RachieVee Oct 2 '14 at 20:18
  • I have the same fields I want to provide on certain posts. I was hoping to make it easy so when I write a post the fields will be there for me to fill in. I looked into the add_post_meta but it sounds like I would need to create a custom post class for that? Sorry if this is a dumb question as im new to wordpress. Thanks! – dmart Oct 3 '14 at 0:19
0

The Custom Field Template plugin will let you define default custom fields for the write post/page screen. Might be a simple solution.

I'm not affiliated with this plugin, just a user.

  • 1
    Please state your affiliation. This is required – Pieter Goosen Oct 3 '14 at 8:45
0

If you want the fields already there without you having to use the dropdown to get them, try using a function with add_post_meta that will add it without a value until you update your post. Try this:

function add_custom_meta(){
  global $post; //make $post global
  $post_id = $post->ID; //grab IDs of the current post

  add_post_meta( $post_id, 'difficulty', '', true );
  /* add the "difficulty" custom field to all posts with empty value */
}

add_action('update_postmeta', 'add_custom_meta');

You don't have to use a custom post type. This is using the standard posts. It makes the $post global so we can access all posts and their post IDs, then plug those IDs into add_post_meta to add our field to all posts. We're also hooking into the update_postmeta to make sure this field gets added before the post meta gets updated.

So when you open a post to edit, this field should be there for you to provide the value and then you can update the field and your post. For a new post however, you have to publish it first or grab it from the dropdown. Perhaps using another hook will fix this but not sure which one. Hope that this leads you in the right direction though. Good luck!

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.