Title: mysql custom query
Last modified: August 20, 2016

---

# mysql custom query

 *  [John Mido](https://wordpress.org/support/users/h3o-co/)
 * (@h3o-co)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/mysql-custom-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

Viewing 6 replies - 1 through 6 (of 6 total)

 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/mysql-custom-query/#post-2916581)
 * Try it with:
 *     ```
       $tags = get_terms('post_tag');
       ```
   
 * [http://codex.wordpress.org/Function_Reference/get_terms](http://codex.wordpress.org/Function_Reference/get_terms)
 *  Thread Starter [John Mido](https://wordpress.org/support/users/h3o-co/)
 * (@h3o-co)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/mysql-custom-query/#post-2916586)
 * [@keesiemeijer](https://wordpress.org/support/users/keesiemeijer/) thanks for
   response.
    but, i don’t think this helps in my case! the code is to filter posts
   in custom template and submit custom user’s query.. so, the first code is to 
   display select menu of meta values to let user choose the desired value and submit
   the form with it, to re filter the posts..
 * in the same way i want to display some tags (manually) to let the user choose
   from them and submit his selection with the rest of the query.
 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/mysql-custom-query/#post-2916600)
 * Not sure I understand. get_terms returns all tags manually assigned to posts.
 *     ```
       <?php
       $tags = get_terms('post_tag');
       $html = '';
       if($tags) {
       $html .= '<select name="tags">';
       foreach ($tags as $tag) {
       	$html .= '<option value="'.$tag->slug.'">'.$tag->name.'</option>';
       }
       $html .= '</select>';
       }
       echo $html;
       ?>
       ```
   
 *  Thread Starter [John Mido](https://wordpress.org/support/users/h3o-co/)
 * (@h3o-co)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/mysql-custom-query/#post-2916609)
 * I just need the opposite of what you did now
 * i want to write the $tag->slug manually, to be passed to $tags when submitting
   the form
    something like this:
 *     ```
       <?php $tags = get_terms('post_tag');?>
       <select name="tags">
       	<option value="tag1">tag1</option>
       	<option value="tag1">tag2</option>
       	<option value="tag1">tag3</option>
       </select>
       ```
   
 * i’ve tried it and not working
 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/mysql-custom-query/#post-2916618)
 * Still not understanding your question. Need more coffee 🙂
 * You want a dropdown with all tags in it, or not?
    where do you want to write 
   the $tag->slug in for example: `<option value="tag1">tag3</option>`
 *  Thread Starter [John Mido](https://wordpress.org/support/users/h3o-co/)
 * (@h3o-co)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/mysql-custom-query/#post-2916636)
 * i think i’m the one who need coffe sorry for this confusion 🙂
    What i want is
   not display the slug or even list tags, what i want is to pass the value of the
   selected option to $tags variable, and i’ll write tags manually in options. The
   purpose here is not listing tags for user, the purpose is allowing the visitor
   to choose the tag to list the posts that have that tag after submitting the form
   which include that droplist Hope its clear now, and thanks alot for your efforts
   with me.

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘mysql custom query’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 6 replies
 * 2 participants
 * Last reply from: [John Mido](https://wordpress.org/support/users/h3o-co/)
 * Last activity: [13 years, 11 months ago](https://wordpress.org/support/topic/mysql-custom-query/#post-2916636)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
