• Resolved danpacey

    (@danpacey)


    Hi all.

    I am running the following code to generate a drop-down box, used to search by a custom field called “area”.

    <form method="get" action="<?php bloginfo('home'); ?>/">
    <select value="<?php the_search_query(); ?>" name="s" id="s" tabindex="1" size="1">
    <option value="">Select an Area</option>
    <?php query_posts('cat=3,4,5,6,7,8,9&showposts=50&orderby=author'); ?>
    <?php while (have_posts()) : the_post(); ?>
    <option value="<?php echo get_post_meta($post->ID, 'area', true); ?>"><?php echo get_post_meta($post->ID, 'area', true); ?></option>
    <?php endwhile; ?>
    <?php wp_reset_query(); ?>
    
    </select>
    <input type="hidden" name="key" value="area" />
    <input type="submit" id="searchsubmit" value="Go" />
    </form>

    The problem i am having (as you’d expect!) is that this returns all the values in that field, so that I have multiple entries of each search term.

    ie: if i have 3 posts with “area” set to “Banana”, i will get 3 “Banana”‘s in my search drop box.

    I’m afriad this is a bit beyond my expertise.. Can anyone help me out with a way to fix this issue?

    Many thanks.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter danpacey

    (@danpacey)

    ok at the moment i am trying to run this code, but it’s not generating any results.

    <?php $querystr = "
    SELECT DISTINCT wp_posts.* FROM wp_posts
    LEFT JOIN $wpdb->postmeta ON($wpdb->posts.ID = $wpdb->postmeta.post_id)
    LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
    LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
    WHERE $wpdb->term_taxonomy.term_id = 3,4,5,6,7,8,9
    AND $wpdb->term_taxonomy.taxonomy = 'category'
    AND $wpdb->posts.post_status = 'publish'
    AND $wpdb->postmeta.meta_key = 'area'
    ORDER BY $wpdb->postmeta.meta_value ASC
    
     ";
    
     $pageposts = $wpdb->get_results($querystr, OBJECT);
    
    ?>
     <?php if ($pageposts): ?>
      <?php foreach ($pageposts as $post): ?>
        <?php setup_postdata($post); ?>
    
    	<option value="<?php echo get_post_meta($post->ID, 'area', true); ?>"><?php echo get_post_meta($post->ID, 'area', true); ?></option>
    
    	<?php endforeach; ?>
    <?php endif ?>

    any ideas?

    will keep trying in the meantime!

    Thread Starter danpacey

    (@danpacey)

    not getting anywhere fast with this, can anyone offer any advice please? thanks guys. Seems like a useful function, if it can be done!

    Thread Starter danpacey

    (@danpacey)

    ok, an update. I’m getting the right amout of results from this code, but not the actual values.

    <?php $querystr = "
    SELECT DISTINCT wp_postmeta.* FROM wp_postmeta
    WHERE $wpdb->postmeta.meta_key = 'area'
    
     ";
    
     $pageposts = $wpdb->get_results($querystr, OBJECT);
    
    ?>
     <?php if ($pageposts): ?>
      <?php foreach ($pageposts as $post): ?>
        <?php setup_postdata($post); ?>
    
    	<option value="<?php echo get_post_meta($post->ID, 'area', true); ?>"><?php echo get_post_meta($post->ID, 'area', true); ?></option>
    
       <?php endforeach; ?>
    <?php endif ?>1

    I have 9 “area” values in the wp_postmeta table at present, and i’m getting a list with 9 <option> tags.. but there is nothing in-between the option tags..

    putting this here in case anyone has any idea what i’m doing wrong. Many thanks again.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Search using Select Option drop box populated by Custom Field’ is closed to new replies.