mysql custom wp query

this code is designed to select posts with selected meta value in wordpress query

<?php $values = $wpdb->get_results("SELECT DISTINCT meta_value FROM $wpdb->postmeta WHERE meta_key  = 'wpcf-scr'",ARRAY_A);?>
<select name="wpcf-scr">
<option value="">default</option>
<?php foreach ($values as $value):?>
<?php if($value['meta_value']):?>
    <option value="<?php echo $value['meta_value']?>"><?php echo $value['meta_value']?></option>
<?php endif;?>
<?php endforeach;?>
</select>

I need to re use the code but with selecting posts with tags(manual assigned) not meta values…

something like this below just for explanation (it’s wrong)

<?php $values = $wpdb->get_results("SELECT DISTINCT post_tags FROM $wpdb->tags ",ARRAY_A);?>
<select name="tags">
<option value="">default</option>
<option value="tag1">tag1</option>
<option value="tag1">tag2</option>
<option value="tag1">tag3</option>
<option value="tag1">tag4</option>
</select>

How to make the second example correctly ?

thanks

2 Answers
2

You could use the wp_dropdown_categories() function and pass post_tag as taxonomy parameter.

<?php wp_dropdown_categories( array( 'taxonomy' => 'post_tag' ) ); ?>

Leave a Comment