Viewing 1 replies (of 1 total)
  • Thread Starter MAD000

    (@mad000)

    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]

Viewing 1 replies (of 1 total)

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