Just getting into wordpress and learning a lot! I know how to create a custom field and use it for basic purposes. I'm trying to do a little more now...I'm hoping someone can help me.

Here is the scenario:

I have businesses and events section on my website. Businesses use the default "posts" from wordpress, "events" are a custom post type.

I want to create a custom field to use for events that populates a dropdown list of the business posts the current user is the author of, effectively linking the two together. This way the user can only see the business posts they've created in the list and can not create events and link them to random businesses.

I've tried utilizing the "LittlePromoBox" class found in this post. But have had no luck...I've tried to understand the logic but its too advanced for me at this point..can anyone help me out?

1 Answer 1


here: What you need is to create a meta box so first you use the add_meta_box()

// Hook into WordPress add_meta_boxes action
add_action( 'add_meta_boxes', 'add_Businesses_custom_metabox' );

 * Add meta box function
function add_Businesses_custom_metabox() {
    add_meta_box( 'custom-metabox', __( 'Businesses' ), 'Businesses_custom_metabox', 'events', 'side', 'high' );

here you can see that i used "events" as the post type i want to register this meta box to and that my callback function is: Businesses_custom_metabox() and its the function that actually displays the metabox, so we define it like this:

 * Display the metabox
function Businesses_custom_metabox($post) {
    global $post,$current_user;
    //remember the current $post object
    $real_post = $post;
    //get curent user info (we need the ID)
    //create nonce
    echo '<input type="hidden" name="Businesses_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
    //get saved meta
    $selected = get_post_meta( $post->ID, 'a_businesses', true );
    //create a query for all of the user businesses posts
    $Businesses_query = new WP_Query();
                            'post_type' => 'posts',
                            'posts_per_page' => -1,
                            'author' => $current_user->ID));
    if ($Businesses_query->have_posts()){
        echo '<select name="a_businesses" id="a_businesses">';
        //loop over all post and add them to the select dropdown
        while ($Businesses_query->have_posts()){
            echo '<option value="'.$post->ID.'" ';
            if ( $post->ID == $selected){
                echo 'selected="selected"';
            echo '>'.$post->post_title .'</option>';
        echo '<select>';
    //reset the query and the $post to its real value
    $post = $real_post;

I tried to comment ever bit of it so you can understand better. And the last thing you have to do is process the metabox when a post is saved:

//hook to save the post meta
add_action( 'save_post', 'save_Businesses_custom_metabox' );
 * Process the custom metabox fields
function save_Businesses_custom_metabox( $post_id ) {
    global $post;
    // verify nonce
    if (!wp_verify_nonce($_POST['Businesses_meta_box_nonce'], basename(__FILE__))) {
        return $post_id;
    // check autosave
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return $post_id;
    // check permissions
    if ('events' == $_POST['post_type']) {
        if (!current_user_can('edit_page', $post_id)) {
            return $post_id;
    } elseif (!current_user_can('edit_post', $post_id)) {
        return $post_id;

    if( $_POST ) {
        $old = get_post_meta($post_id, 'a_businesses', true);
        $new = $_POST['a_businesses'];
        if ($new && $new != $old){
            update_post_meta($post_id, 'a_businesses', $new);
  • There's a metabox action specifically for registering meta boxes you know? (add_meta_boxes action).. Additionally metabox callback functions for posts receive the $post object as the first parameter, eg. function Businesses_custom_metabox( $post )
    – t31os
    Mar 11, 2011 at 8:52
  • WOW thanks for taking the time to help me out! I added this to my functions.php and it displays a "Bussinesses" metabox but no dropdown of posts..was there something I was to edit above for my situation?
    – Mike
    Mar 11, 2011 at 14:38
  • Also, how could I add this to my frontend form for users to submit events?
    – Mike
    Mar 11, 2011 at 14:39
  • @t31os - as always thanks for the clarification, i updated the code , and i don't know why i keep forgetting about add_meta_boxes "action" hook.
    – Bainternet
    Mar 11, 2011 at 14:45
  • @Mike you must have businesses posts created by the same user you are logged in with to see any of them in the dropdown. and as for showing it on the front end just rewrite Businesses_custom_metabox function to fit the front-end needs
    – Bainternet
    Mar 11, 2011 at 14:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.