WordPress.org

Forums

[resolved] Custom database query selecting posts of one category (7 posts)

  1. jkmuller
    Member
    Posted 7 years ago #

    I'm trying to create a database query which selects posts of a certain category and displays their titles and link. I've got everything working except the category selection feature. It seems that all the posts, regardless of what category they are in, show up with a post_category of 0 in the posts database. How can I select posts of a certain category? The code I'm using is below.

    Thanks,
    Joe

    <?php
    	global $posts;
    	if ( $post= $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_type='post' AND post_category=6 ORDER BY post_date_gmt DESC LIMIT 5") ) :
    	?>	<ul class="list-cat">
    <?php foreach ($post as $pst) {
    		echo '<li><a href="'.$pst->guid.'">'.$pst->post_title.'</a></li>';
    	}
    	?>
    	</ul>
    	<?php endif; ?>
  2. moshu
    Member
    Posted 7 years ago #

    Aren't you overcomplicating it?
    Why not just edit out/delete the_content tga from a category template and you are done.

  3. Yeah, that's way overkill. Never do custom SQL when the code can do it for you.

    <?php
    query_posts('cat=6&showposts=5');
    if (have_posts()) {
    echo '<ul class="list-cat">';
    while (have_posts()) { the_post();
    ?><li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php
    } // end while loop
    echo '</ul>';
    } // end if
    wp_reset_query();
    ?>
  4. jkmuller
    Member
    Posted 7 years ago #

    Unless I'm misunderstanding the category template, I think I need something more. I'm trying to populate a list with recent posts of a certain category on the main page of a blog. This seemed like the simplest solution to me, but I'm not extremely familiar with WordPress.

    The site is http://f-investors.com/, and the area I am trying to create a list in is below the "What's New" header at the bottom left of the page.

  5. jkmuller
    Member
    Posted 7 years ago #

    Excellent Otto, that did the trick. I can never figure out the best way to implement something. Thanks!

  6. fishnyc
    Member
    Posted 7 years ago #

    I'm looking to do something similar here. Is it possible in v2.5?
    Thx

  7. Cregy
    Member
    Posted 6 years ago #

    Hi

    I am trying to do something similar. I actually have a suckerfish menu that I am trying to list the last 10 posts from a certain category. Here is the code:

    <li><a href="http://www.uniquecards.org/category/print-training-and-tips/">Print Training and Tips</a>
    <ul>
    <?php
    query_posts('cat=3&showposts=5');
    if (have_posts()) {
    while (have_posts()) { the_post();
    ?><li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php
    } // end while loop
    } // end if
    wp_reset_query();
    ?>
    </ul>
    </li>

    And a link to the page:
    The Page

    As you can see no drop down is created. Any ideas why please?

    Thanks

    Rich

Topic Closed

This topic has been closed to new replies.

About this Topic