Title: WP Query &#8211; Custom Query
Last modified: August 19, 2016

---

# WP Query – Custom Query

 *  [MAD000](https://wordpress.org/support/users/mad000/)
 * (@mad000)
 * [16 years, 10 months ago](https://wordpress.org/support/topic/wp-query-custom-query/)
 * Removed to WP advance.

Viewing 1 replies (of 1 total)

 *  Thread Starter [MAD000](https://wordpress.org/support/users/mad000/)
 * (@mad000)
 * [16 years, 10 months ago](https://wordpress.org/support/topic/wp-query-custom-query/#post-1174201)
 * Hi all,
    I have been trying to find a simpler, more efficient way to display 
   different posts depending on different conditions. For now I wrote the following
   script to retrieve posts for a specific category named ‘Inspiration’ and that
   should be displayed in descending order based on the number of comments (popularity).
   I would welcome any suggestions to shorten this (I feel there is another way!!)
   here is the code:
 *     ```
       <div class="inspirational-post-box"><!-- start -->
       <h1 class="heading-small">Inspirational Posts</h1>
       <?php $sql = "SELECT * "
       	. "FROM $wpdb->posts AS p "
       	. "WHERE p.post_status = 'publish' "
       	. "AND p.post_type = 'post' "
       	. "AND p.ID IN (SELECT tr.object_id "
       	. "FROM $wpdb->term_relationships tr "
       	. "WHERE tr.term_taxonomy_id IN (SELECT tt.term_taxonomy_id FROM wp_term_taxonomy AS tt "
       	. "INNER JOIN $wpdb->terms AS t ON t.term_id = tt.term_id "
       	. "WHERE (tt.term_id = (SELECT tt.term_taxonomy_id "
       	. "FROM $wpdb->term_taxonomy AS tt "
       	. "INNER JOIN $wpdb->terms AS t ON tt.term_id = t.term_id "
       	. "WHERE tt.taxonomy = 'category' AND t.name = 'Inspiration') OR tt.parent = (SELECT tt.term_taxonomy_id "
       	. "FROM $wpdb->term_taxonomy AS tt "
       	. "INNER JOIN $wpdb->terms AS t ON tt.term_id = t.term_id "
       	. "WHERE tt.taxonomy = 'category' AND t.name = 'Inspiration')))) "
       	. "ORDER BY p.comment_count DESC LIMIT 12";
       $result = $wpdb->get_results($sql);
       foreach ($result as $post) {
       setup_postdata($post);
       ?>
       <div class="entry-small">
       <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><img class="thumbnail-small" src="<?php bloginfo('template_url') ?>/images/<?php echo get_post_meta($post->ID, 'post-thumbnail', true); ?>" width="50" height="50" alt="Small Thumbnail" /></a>
       <h2 class="title-small">
       <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
       </h2>
       </div>
       <?php } ?>
       </div><!-- end -->
       ```
   
 * [**signature moderated** Please read the [Forum Rules](http://wordpress.org/support/topic/68664?replies=6)]

Viewing 1 replies (of 1 total)

The topic ‘WP Query – Custom Query’ is closed to new replies.

## Tags

 * [custom-query](https://wordpress.org/support/topic-tag/custom-query/)
 * [wpdb](https://wordpress.org/support/topic-tag/wpdb/)

 * 1 reply
 * 1 participant
 * Last reply from: [MAD000](https://wordpress.org/support/users/mad000/)
 * Last activity: [16 years, 10 months ago](https://wordpress.org/support/topic/wp-query-custom-query/#post-1174201)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
