Title says it all, I pretty much just want to put a dropdown list on the front end that will list all the posts of custom post type 'agent', preferably in alphabetical order, and go to that post when user clicks on it.


try this:

//first get all agents
$agents = new WP_Query(array('post_type' => 'agent', 'posts_per_page' => -1, 'order' => 'ASC', 'orderby' => 'title'));
//then just print out the dropdown:
if ($agents->have_posts()){
    echo '<select id="agent">';
    while ($agents->have_posts()){
        echo '<option value="'.get_permalink($post->ID).'">'.get_the_title($post->ID).'</option>';
    <script type="text/javascript">
        var dropdown = document.getElementById("agent");
        function onCatChange() {
            if ( dropdown.options[dropdown.selectedIndex].value > 0 ) {
                location.href = dropdown.options[dropdown.selectedIndex].value;
        dropdown.onchange = onCatChange;
  • It appears that loading 77 posts like this takes too long or something is not quite right with the script, when I add this to the page it seems to attempt to load all the names, but all I get is a narrow dropdown with empty data and a scrollbar. I tried removing the js in case, but had no affect. Check it out here, test.medialayer.net, it's the second dropdown under find and agent. – Chuck Jul 5 '11 at 10:50
  • @Chuck 77 posts should be that bad and you can always save the output as a transient , can you create a pastebin of the exact code you are using? – Bainternet Jul 5 '11 at 11:00
  • here is my pastebin. pastebin.com/n2CeVYrx Thanks. – Chuck Jul 5 '11 at 14:32
  • I deleted that last comment with the pastebin, as I realized I was doing it completely wrong I have a new pastebin, pastebin.com/X4fDHwvL which is as far as I've gotten so far, if you have a moment to take a look and at least let me know if I'm going the right direction I would be grateful. – Chuck Jul 6 '11 at 18:04
  • I'LL take a look as soon as get home – Bainternet Jul 6 '11 at 19:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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