1

I want to add custom field, named score, for each post individually and store it in database so I can later order posts by that field. I need to make it available on post edit to change this value, so it would basically be an input text field which accept numbers 0 to 5. To do this, do I have to create custom post type, or is there a way to achieve it with ordinary posts section?

4
  • Adding a custom field is a good question to ask, but sorting by that field is a separate new question. You're much more likely to get answers if you remove sorting and ask a new question: "How to sort the posts list by a custom field?" otherwise you put off people who know the answer to one part but not the other, and intimidate people who don't have time to research both for a single answer.
    – Tom J Nowell
    Aug 31 '20 at 19:11
  • Thank you, this was for my job interview where wordpress is used and I have never used it before, and was short on time to do a slightly bigger research. If I had, I wouldn't even ask it, because it's pretty straight forward - create custom field for posts, it's going to be stored as postmeta and later order by that postmeta value
    – nikname
    Sep 1 '20 at 8:35
  • Even then, nothing stops you asking multiple questions in parallel. Don't put all your eggs in a single basket! Not everybody can eat an entire basket of eggs at once. Lots of small separate baskets though you'll get lots more answers from lots of different people. Also be mindful everything here is public, a potential employer can see all of this
    – Tom J Nowell
    Sep 1 '20 at 9:40
  • I agree with you totally! Thanks for advice, I will sure follow it next time asking a question. If there were more patient people like you are, the world would be much better place. On a question like this I would more expect criticism than advice. Thanks again!
    – nikname
    Sep 2 '20 at 8:34
0

The easiest approach would be to use the Advanced Custom Fields plugin.

A quick example for your situation (this example assumes you have access to your theme files, and can update the PHP for output of the 'Scores' field):

  1. Install and activate the plugin
  2. Navigate to Custom Fields in the left-hand nav
  3. Create a new Field Group
  4. Create a 'Scores' text field
  5. Output the 'Scores' field in your page/post
  6. Use WP_Query to load and sort the posts by your new 'Score' field

Good luck!

4
  • 2
    I was thinking of doing it without plugin, but thanks. I searched a little bit and I think that what I need is add_meta_box function
    – nikname
    Aug 31 '20 at 18:16
  • 1
    Keep in mind answers should be self contained. You can use ACF as an example and link to it, but ACF cannot be the answer itself. If the OP went with this answer, they would never have found out lots of super important things, e.g. that custom post types and posts are the same thing, they would never have found out about the post meta table or functions such as get_post_meta, the metabox API, or the more modern methods with block meta, block panels, or exposing data in the REST API for javascript, or register_meta, etc, or even the dangers of searching for posts via their meta values
    – Tom J Nowell
    Aug 31 '20 at 19:09
  • Hi Tom - Thanks for the feedback. Can you please elaborate on what "self-contained" means in this context? I don't see any reason why "ACF" isn't a perfectly acceptable answer to OP's question about wanting to add a custom field for each of their posts.
    – Pixelsmith
    Aug 31 '20 at 23:08
  • @Pixelsmith I agree with Tom - although I love ACF, it's really overkill for this particular OP's need. I would have suggested a simpler solution which would be to add the few lines of code needed to add a custom metabox (to a Child Theme's functions.php), then filtering the loop to find the metadata and sort on it. Plugins do add weight to every page and can slow page-load-speed so I generally only recommend them when there isn't a lightweight alternative that's easy for most developers to implement. These days Google will down-rank a site for even nanoseconds of extra load speed.
    – Trisha
    Sep 1 '20 at 19:33

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.