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 have the following issue:

I am creating a custom-meta-box for the user to add custom fields in a friendly way.
I want to generate a drop-down box with all posts that fall under category 10.
I have created a loop inside the functions file for retrieving the posts I want and to create a dropdown select list.

So when I go to admin -> posts and select a post to edit, I get the following issue:
the last post will load no matter which one I select.

Here is the code used inside the functions file and then called when generating the html code for the custom meta-box:

function get_project_ids($proj_cat = 10) {

$output = '';

$catt = get_categories('parent='.$proj_cat.'&hide_empty=0');

    foreach ($catt as $c) :

        $output .= '<optgroup label="'.$c->name.'">';
        $d = get_categories('parent='.$c->cat_ID.'&hide_empty=0');

            foreach ($d as $e) :
            $output .= '<optgroup label="'.$e->name.'">';

                $args = array( 'nopaging' => true, 'cat' => $e->cat_ID );
                $project_query = new WP_Query( $args );

                    while ( $wp_query->have_posts() ) : $wp_query->the_post();
                        $project_id = (get_post_meta($post->ID, 'project_code', true) != "")?get_post_meta($post->ID, 'project_code', true):"";
                        $output .= '<option value="'.$post->ID.'">'.$project_id.'-'.get_the_title().'</option>';    
                    endwhile; 

                wp_reset_postdata();
                $wp_query = null;
                $wp_query = $original_query;

            $output .= '</optgroup> <!-- END level-b -->';
        endforeach;

        $output .= '</optgroup> <!-- END level-a -->';
    endforeach;

return $output;
share|improve this question
    
Where does $e come from? Second: Where's the original $output you're .= adding too? Third: Why are you setting $wp_query to null? No reason to do that. –  kaiser Jul 3 '11 at 21:28
    
ok you're right I should be more clear. (i) $e comes from a foreach loop and is the id of the category (ii) $output is initialized a few lines back. (iii) I'm actually following an example from codex where the original wp query is temporary put aside. ... The thing is that this query for some reason forces the same post in the admin edit post page. –  ion Jul 3 '11 at 23:44
    
Could you please simply post your whole code? And then add the information to your Q? Hint: You can edit your Q. Hint #2: You have a "help" link for commenting and such that will you show you the mark-up stuff like code in comments and else. Thanks. –  kaiser Jul 4 '11 at 1:51
add comment

3 Answers

I am not sure about the issue, but my advice would be to try and refactor this to use get_posts() and template tags that can work without $post global variable. Basically do not touch globals at all.

Loops in front-end are almost civilized nowadays, but internals of admin are still very wild. :)

share|improve this answer
    
Great man!!!! It worked ... I will edit my original post with the working code –  ion Jul 4 '11 at 11:11
    
@ion it is more proper to add working solutions as new answer (even for own question), that is how site is supposed to work –  Rarst Jul 4 '11 at 11:24
add comment

You're creating a new query, called $project_query, then never using it.

Then you're using $wp_query, for which there is no global $wp_query; statement.

Then you're assigning null to $wp_query then assigning the value of an undefined variable to it.

Instead remove the two unnecessary lines and use $project_query instead. here's the modified query loop:

            $args = array( 'nopaging' => true, 'cat' => $e->cat_ID );
            $project_query = new WP_Query( $args );

                while ( $project_query ->have_posts() ) : $project_query ->the_post();
                    $project_id = (get_post_meta($post->ID, 'project_code', true) != "")?get_post_meta($post->ID, 'project_code', true):"";
                    $output .= '<option value="'.$post->ID.'">'.$project_id.'-'.get_the_title().'</option>';    
                endwhile; 

            wp_reset_postdata();
share|improve this answer
add comment

This is the working code:

global $post;
$tmp_post = $post;
$args = array( 'numberposts' => 10000, 'category' => $e->cat_ID );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post);
    $project_id = (get_post_meta($post->ID, 'project_code', true) != "")?get_post_meta($post->ID, 'project_code', true):"";
    $output .= '<option value="'.$post->ID.'">'.$project_id.'-'.get_the_title().'</option>';    
endforeach;
$post = $tmp_post;
share|improve this answer
    
Mark your question as answered. –  toscho Feb 14 '12 at 11:52
add comment

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.