WordPress.org

Support

Support » Plugins and Hacks » Hacks » Help! Trying to list Custom Posts as list!

Help! Trying to list Custom Posts as list!

  • deziiner

    @deziiner

    Hey guys, I really hope some one can help!

    I’m using custom post types to create a bands (profiles) database, and what I want to do in my backend panel is be able to select a ‘profile’ and save the value. So I’m trying to query all of my custom post types (doubledance_bands) and display them in a <select> box. Here’s my code I’ve been using…

    <label for="optionArtistselect">Choose an artist profile!</label>
                    <select id="optionArtistselect" name="optionArtistselect">
                        <?php
                        $profiles = new WP_query('post_type=doubledance_bands');
                        foreach ($profiles as $profile) {
                            echo '<option value="' . ($profile->ID) . '">' . ($profile->post_title) . '</option>';
                        }
                        ?>
                    </select>

    Forgive me, as I’m not the best at working in WordPress… the outputted code with that is this:

    <label for="optionArtistselect">Choose an artist profile!</label>
                    <select id="optionArtistselect" name="optionArtistselect">
                        <option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value="2551">Chiodos</option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option>                </select>

    Can anyone help me?

Viewing 4 replies - 1 through 4 (of 4 total)
  • I haven’t touched custom posts but if I’m not mistaken you’re supposed to do something like this for the loop

    $profiles = new WP_query(‘post_type=doubledance_bands’);
    while ($profiles->have_posts()) {
    $profiles->the_post();
    echo ‘<option value=”‘;
    the_ID();
    echo ‘”>’;
    the_title();
    echo ‘</option>’;
    }

    I don’t normally use template tags so the output part might not be correct but the key thing is what WP_Query returns doesn’t seem to be an array from the documentation.

    deziiner

    @deziiner

    That worked brilliant! You wouldn’t have any idea on how to save the selected data, and then upon page reload, the selected artist is automatically the selected option?

    deziiner

    @deziiner

    I’ve tried to use the following:

    $profiles = new WP_query(‘post_type=doubledance_bands’);
    while ($profiles->have_posts()) { $profiles->the_post();
    if($profiles->ID==get_option(‘optionArtistselect’)) { $selected = ‘ selected’; } else { $selected = ”; }
    echo ‘<option value=”‘ . the_ID();
    echo ‘”‘;
    echo $selected;
    echo ‘>’;
    the_title();
    echo ‘</option>’;
    }

    … unfortunately it simply outputs all the options as ‘selected’! Any ideas?

    Keep in mind, $profiles is not the post itself, so $profiles->ID probably returns true or something unexpected so you end up with all selected trying to use it for comparison.

    You probably have to do a $currentPost=get_post(the_ID()) or something then use $currentPost to pull the value for comparison. Sorry can’t give details now because I’m on the move, lousy connection 😀

    Will try to give a better response later if you haven’t figure it out by then.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Help! Trying to list Custom Posts as list!’ is closed to new replies.