Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want to retrieve a specific post_meta of all posts contained in a specific category. I want to build a search form at the top of each category with a select field which would be filled up by the required post_meta of all the post contained in the category the user is currently in. Is this doable? Maybe it's possible with a loop which would look like:

For each post_meta called "City" in all posts contained in category 'X', you must put it in <option value="City's name">City's name</option>

So far, here is what I have been able to do.

<select>
<option value="" selected="selected">Please pick a city below...</option>
<option value="<?php echo get_post_meta($post->ID, 'City', true) ?>">
<?php echo get_post_meta($post->ID, 'City', true) ?>
</option>
</select>

Not much, I know. Any help, please ? Thank you in advance.

share|improve this question

3 Answers 3

I sounds like your doing this wrong. You do not typically query the post meta first in a hierarchical query since it will be much slower. I don't really know how your structuring this by your question.

The logic should be -->query category--->query posts with meta value ( or tag or sub-category, don't forget these work to).

To do this you need to use WP Query http://codex.wordpress.org/Class_Reference/WP_Query

For example $query = new WP_Query( 'cat=4&meta_key=city' );

If your using a lot of meta field values you should set get_post_meta to false or leave that parameter out to return an array ( your example has it set to true).

share|improve this answer
    
Please check the answer I posted, especially the link to the article "Enhancing WordPress Custom Fields with Search Filtering". That is what I meant to do. I'm sorry if my explanation wasn't clear. –  Macxim Jul 20 '11 at 11:03

Give this a try:

<?php
$args = array( 'category' => 3, 'meta_key' => 'City' );
$lastposts = get_posts( $args );
echo '<select>';
echo '<option value="" selected="selected">Please pick a city below...</option>';
foreach($lastposts as $post) : setup_postdata($post);
    $themeta = get_post_meta($post->ID,'City',true); ?>
    <option value="<?php echo $themeta; ?>"><?php echo $themeta; ?></option>
<?php endforeach;
echo '</select>';
?>

I didn't test it but it should work. It's getting all posts from a certain category along with a certain meta key.

share|improve this answer
    
I tested it but it doesn't seem to work. It doesn't even display the select field. Am I doing anything wrong? –  Macxim Jul 20 '11 at 11:01
up vote 0 down vote accepted

Thank you very much for your answers!

I finally found what I wanted here : http://sixrevisions.com/wordpress/custom-fields-search/ Here is the code :

<form name="search" action="" method="get">
<select name="City">
 <?php
  $metakey = 'city';
  $cities = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT meta_value FROM $wpdb->postmeta WHERE meta_key = %s ORDER BY meta_value ASC", $metakey) );
  if ($cities) {
    foreach ($cities as $city) {
  echo "<option value=\"" . $city . "\">" . $city . "</option>";
    }
    }
 ?>
</select>
<input type="submit" value="search" />
</form>

This is a brilliant article and it helped me a lot! I hope it'll help others too.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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