Leave a Reply

2 comments

  1. When hooking the save_post action, you really need to isolate your operations so that every page/post/post_type save doesn’t try to add your post meta to the post.

    function foo_save(){
        if(isset($_REQUEST['bar')
            update_post_meta($_REQUEST['post_ID'], 'bar', $_REQUEST['bar']);
    }
    add_action('save_post', 'foo_save');
    

    The main issue with your code above is posts_numericalvalue needs quotes. Also, to use $post->ID you will need to make sure your code is in the loop or declare global $post; somewhere above your code if you’re using it outside of the loop.

    if(have_posts()):
        while(have_posts()):
            the_post();
            $foo = get_post_meta($post->ID, 'bar', true);
            echo $foo;
        endwhile;
    else:
    
    endif;
    

    Also, if you are distributing this plugin to the public, you will need to create a function to filter the content and add your post meta there since you won’t have true access to the template files.

    If this is just for your own use in your theme, I would place the function foo_save() in your functions.php file instead.

    Hope this helps you out!