• Resolved lhinds

    (@lhinds)


    Hi,

    I am new to wordpress development, so please excuse any basics I may not have grasped as yet.

    I have the following code to create a meta box which I wish to have populated with one checkbox.

    The code is rendering the meta box on the edit post page and I can see it is saved to the database. If I select the checkbox, it saves and is retained on refresh, so this all works. I can see these entries in the wp_postmeta

    [Code moderated as per the Forum Rules. The maximum number of lines of code that you can post in these forums is ten lines. Please use the pastebin]

    I now want to construct a statement which will check the DB for the meta_value (‘on’ or ‘off’)for my meta_key ‘my_meta_box_check’ and use that value to process accordingly within an if statement.

    So far the best I have come up with from searching around is:

    if ( get_post_meta($post->ID,’my_meta_box_check’, true ) == “on” ){
    echo “checkbox selected”

    Trying this gives me:

    Notice: Undefined variable: post in C:\sitedev\xampp-dev\xampp\htdocs\wordpress\wp-content\plugins\testplugin\testplugin.php on line 30
    
    Notice: Trying to get property of non-object in C:\sitedev\xampp-dev\xampp\htdocs\wordpress\wp-content\plugins\testplugin\testplugin.php on line 30

    * Line 30 is the get_post_meta if statement I have above.

    I am pretty much stuck now. There are lots of tutorials around on how to create a meta box, but none on how to query and use the data they store.

    Any help would be much appreciated.

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Rendering stored data from add_meta_boxes (checkbox)’ is closed to new replies.