• Resolved jkmuller

    (@jkmuller)


    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; ?>
Viewing 6 replies - 1 through 6 (of 6 total)
  • moshu

    (@moshu)

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

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.remarpro.com Admin

    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();
    ?>
    Thread Starter jkmuller

    (@jkmuller)

    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 https://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.

    Thread Starter jkmuller

    (@jkmuller)

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

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

    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="https://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

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Custom database query selecting posts of one category’ is closed to new replies.