122 responses to “How to Create A Custom WordPress Meta Box Instead of Using WordPress Custom Fields”

  1. Cippo

    Nice tutorial, it’s working great! Thanks!

    But I have a question for you. How can I use the WP_Query from here: http://codex.wordpress.org/Function_Reference/WP_Query#Custom_Field_Parameters

    I have 2 checkboxes in my template and I want to display the loop if a checkbox is checked.. but I don’t know how to use that in meta_query

    Can you help?

  2. Andrea

    Thank you Dimas ;)

  3. Cippo

    In my rush, I didn’t see how simple it was … d’oh.

    The query part for meta looks like this:

                        'meta_query' => array(
                            array(
                                'key' => '_my_meta',
                                'value' => 'addtosidebar',
                                'compare' => 'LIKE'
                            )
                        )
    

    and I was trying to make it ‘key’ => ‘_my_meta[addtosidebar’ and it didn’t want to work. Yes, I admin that I’m not a PRO at PHP :) .. yet.

    But THANKS for sharing the code, very helpful!

  4. Andrea

    I did not understand how to use the file type in the class, so i continued using your first article.
    i put this to test and does not return to me a value.

    get:

    return:
    $new_file_data = $_FILE['_my_meta'];
    print_r($new_file_data);

  5. Andrea

    forget, i’m a stupid :)
    i forgot the S on $_FILES

  6. Matt Strange

    Hi,

    Really flexible custom meta box solution, thankyou :)

    I’m trying to create a select box, any ideas how to incorporate this with your script?

    Matt

  7. phoenix

    Hi,
    when add checkbox or radio button for example
    with

    jacket
    t-shirt

    update post and add in single.php

    it’s ok but i=when return into admin area and try to change some value all field was reset.
    Why my choice in admin panel is not save?
    Sorry for my English

  8. Matt Strange

    @Dimas

    Thanks for the wpalchemy link, just playing around with it now, must say i’m highly impressed.

    Cheers

  9. Max

    I just put this code after my thesis functions on the functions.php and all my thesis meta boxes disappeared and it just added this meta box… =/

  10. Paul

    Hi Dimas

    Would u be so kind to explain how the my_meta_clean() work ?
    It calls itself inside the loop ?

    i just want to understand how it works, rather than copy and use it.
    any level of explanation is much appreciated.

  11. Paul

    Hi Dimas

    Thanks a whole lot for the explanation and the link to revised code.
    ( really appreciate your help and generosity )

  12. fahim

    hi i am creating a wordpress theme can you please help ?
    the code is not working on the page of wordpress ,it work on the post ,
    the box are show on every post and pages but the values not show in my template ,(yes i use the above code in my template )i check the html they retun nothing but in the post they working well

  13. fahim

    there is nothing a new which i add in your code, i just download the code and put them according to instruction

    i grab the values on the both template page
    a blog template page (index.php) and the single.php a (a single blog post page),i set the index as a home page

    the problem is that when i put the values on the blog page ( a wordpress page not a post ) page not show them

    but when i put the values in the single post then both home page (index.php) and single post page(single.php) show them (both page and the post show the values)

    i dont want to use meta box in post , i want to use them in the pages

    for both page and post the $post->ID Work ?
    i dont know anything about them , In the wordpress both page and post meta data access by get_post_meta function, there is no difference ?

    Please Reply as soon as possible

  14. srinivas

    how to disable drag & drop feature for metabox?

  15. Craig

    Thanks for this, it’s fantastic and very easy to setup.

    I have one problem though, the meta data writes to the database okay, and displays on the site correctly, but when I go back into the Edit Page screen, the custom boxes are blank again.

    Any idea why that would be?

  16. Craig

    Hi, Scrap that!!!

    Just noticed I needed to change the meta names in the meta.php input boxes.

    All working now, fantastic article. Thank you!

  17. Ethan

    Hi, I hope Dimas or someone can assist with a problem I’m having. First let me describe what I’m up to: I am creating a custom post type for ‘portfolio’. Because the small thumbnails in the proposed portfolio galleries are not the same image as the one they will link to. I decided to use wpalchemy to create the custom meta boxes that would allow me to add two images at a time to a ‘portfolio’ post for the purpose of matching up the thumb with the larger image and then being able to drag and drop to reorder them.

    I have also employed scripts to be able to access the wp media loader for each of these custom fields, upload an image and put it’s URL into the custom field.

    Everything works, but one frustrating problem. Upon saving the post, the field sets duplicate so there are two of any recent additions when the edit page reloads… it is creating extra entries for a reason I can’t seem to track down…

    A symptom that may point to part of the problem. When you go to create a new ‘portfolio’ the regular media upload button shows that there are no attachments to the post. However, when the media uploader pops up in relation to filling in the custom fields, it remembers every image that has been uploaded to this custom post type, as if there were attached to the new post… not sure if this is related.

    In any case, there are three documents worth looking at (I posted on GIT hub: https://github.com/seahoss/shareCode), the three documents which contribute are: ‘functions.php’, ‘file-upload.js’, and ‘meta_portfolio_images.php’

    Any help you can provide is greatly appreciated.

  18. sachin

    Thanks its a fantastic help

  19. Alvaro

    Hello! Any example for showing a list posts from a custom post type with custom meta boxes?

  20. Dustin

    Thanks so much for this tutorial!

Leave a Reply