I have a question for you all!

I have a post type called ‘candidato’, another post type called ‘lista’ and another post type called ‘list1’.

Each post type has many custom fields.

In ‘candidato’ the main field is ‘n-cand’ which indicates a unique position. Each ‘candidato’ has his own position, so that the ‘n-cand’ value can identify each specific ‘candidato’.

Each ‘candidato’ is supported by one or more ‘lista’ (one at least). So, in ‘lista’ I created a custom field (through a while loop), with a dropdown list that dynamically updates, based on the number of published ‘candidato’. So if I have 4 ‘candidato’, when I create a new ‘lista’ I have 4 options I can select. Each of this option displays the_title() (that is the name of each ‘candidato’) and assign ‘n-cand’ value (that is the position of ‘candidato’) as value.
I used this way:

<select name="sind-list">

<?php
    query_posts( array(
        'post_type' => 'candidato',
        'orderby' => 'meta_value_num',
        'meta_key' => 'n-cand',
        'order' => 'ASC'
    ) );   

    if( have_posts() ) : while( have_posts() ) : the_post();
?>
    <?php global $post; ?>
    <?php $ncand = get_post_meta($post->ID, 'n-cand', true); ?>

    <option value="<?php echo $ncand; ?>" <?php if ( isset ( $liste_stored_meta['sind-list'] ) ) selected( $liste_stored_meta['sind-list'][0], $ncand ); ?>><?php _e( the_title(), 'prfx-textdomain' )?></option>';
<?php endwhile; endif; wp_reset_query(); ?>
</select>

This works correctly only if I use ‘global $post’.

What I have to do now is to create a meta box also for ‘lista1’, where I have a list of ‘candidato’ ordered by position (‘n-cand’ key) and under each one a list of ‘lista’ which supports that ‘candidato’. That is a loop within a loop.
I used this code:

<?php
    query_posts( array(
        'post_type' => 'candidato',
        'orderby' => 'meta_value_num',
        'meta_key' => 'n-cand',
        'order' => 'ASC'
    ));   

    if( have_posts() ) : while( have_posts() ) : the_post();
?>

    <?php
    global $post;
    $ncand = get_post_meta($post->ID, 'n-cand', true);
    ?>

            <?php
            $postlist = new WP_Query( array(
                 'post_type' => 'lista',
                 'orderby' => 'meta_value_num',
                 'meta_key' => 'n-list',
                 'order' => 'ASC',
                 'meta_query' => array(
                      array(
                      'key' => 'sind-list',
                      'value' => $ncand,
                      'compare' => '='
                      ),
                 ),
            ));
            while( $postlist->have_posts() ) : $postlist->the_posts();
            ?>
            // WRITE SOMETHING
            <?php endwhile; wp_reset_query(); ?>

<?php endwhile; endif; wp_reset_query(); ?>

But this doesn’t work!!!

I get this message:

Fatal error: Call to a member function have_posts() on a non-object

which refers to the last line of the code above.
May anybody help me?
Really thank you!

Read more here: Loop within a loop in meta boxes


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: