WordPress.org

Ready to get started?Download WordPress

Forums

Help! Trying to list Custom Posts as list! (5 posts)

  1. deziiner
    Member
    Posted 3 years ago #

    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?

  2. Xephan
    Member
    Posted 3 years ago #

    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.

  3. deziiner
    Member
    Posted 3 years ago #

    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?

  4. deziiner
    Member
    Posted 3 years ago #

    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?

  5. Xephan
    Member
    Posted 3 years ago #

    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 :D

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

Topic Closed

This topic has been closed to new replies.

About this Topic