Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

i found a code searching through stackexchange --> to create a "list" inside a meta box of a custom type, BUT, i don't see anything appearing in this meta box, except the input field (which comes from the example page from the CODEX : http://codex.wordpress.org/Function_Reference/add_meta_box), is it a problem with WP_QUERY inside a function?

Can you please tell me what is the magic trick to fix this? in my callback function, i added this code :

function myplugin_inner_custom_box() {
  // Use nonce for verification
  wp_nonce_field( plugin_basename( __FILE__ ), 'myplugin_noncename' );

  // The actual fields for data entry
  echo '<label for="myplugin_new_field">';
  _e("Description for this field", 'myplugin_textdomain' );
  echo '</label> ';
  echo '<input type="text" id="myplugin_new_field" name="myplugin_new_field"/>';


 $s_query = new WP_Query( array(
 'suppress_filters' => false,
 'post_type' => 'movies'));
while($s_query->have_posts()):$s_query->the_post();

    $sname = $post->post_title;
    $s_output2 ='';
    $s_output2 .= '<option value="'.$post->ID.'" >';
    $s_output2 .= $post->post_title;
    $s_output2 .= '</option>';
    echo $s_output2;

endwhile ;
wp_reset_query();
}

But as i said, the "list" doesn't appear. Could you please help me?

share|improve this question

1 Answer 1

up vote 0 down vote accepted

You are missing the declaration of the HTML select tag which options are his children so just add something like this:

function myplugin_inner_custom_box() {
    // Use nonce for verification
    wp_nonce_field( plugin_basename( __FILE__ ), 'myplugin_noncename' );

    // The actual fields for data entry
    echo '<label for="myplugin_new_field">';
    _e("Description for this field", 'myplugin_textdomain' );
    echo '</label> ';
    echo '<input type="text" id="myplugin_new_field" name="myplugin_new_field"/>';

    //the actual select tag
    echo '<label for="my_list_field">';
    _e("Description for this field", 'myplugin_textdomain' );
    echo '</label> ';
    echo '<select name="my_list_field" id="my_list_field">';
    $s_query = new WP_Query( array(
    'suppress_filters' => false,
    'post_type' => 'movies'));
    while($s_query->have_posts()):$s_query->the_post();

        $sname = $post->post_title;
        $s_output2 ='';
        $s_output2 .= '<option value="'.$post->ID.'" >';
        $s_output2 .= $post->post_title;
        $s_output2 .= '</option>';
        echo $s_output2;

    endwhile ;
    echo '</select>';
    wp_reset_query();
}
share|improve this answer
    
@Bainternet : Alright thanks a lot, it works with SELECT, however the "post" is not recognized, which let me with blank "option" content, it seems that $post->post_title or any of the $post is not recognized. Do you know what i could do for that? edit:i tried with "the_title()" or "the_ID" but it's not working. –  Paul_p May 24 '11 at 22:16
    
@Paul_p declare global $post; at the beginning of the function. –  kaiser May 24 '11 at 22:44
    
@Bainternet : no, thanks but it doesn't work. "post_title" is in green in Netbeans, which normally means that it doesn't exist, or is not declared. What would you do? post_title and ID are in green... –  Paul_p May 25 '11 at 2:14
    
They are green because they are a part of the $post Object which is generated every time you use the_post() inside a loop , and i guess that Netbeans doesn't know that :) –  Bainternet May 25 '11 at 7:01
    
@Bainternet : Ok maybe the time for the navigator to 'refresh', but now it works well! :) thanks a lot for your help! Cheers –  Paul_p May 25 '11 at 21:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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