WordPress.org

Ready to get started?Download WordPress

Forums

SELECT only certain categories (7 posts)

  1. thebindlestick
    Member
    Posted 7 years ago #

    How do make this select statement NOT select posts from the category 3? I mean what is the syntax, I have tried everything. I have my category 3 displayed separately, this is for my list of the 10 most recent posts on my right column.

    <?php
    
    $today = current_time('mysql', 1);
    
    if ( $recentposts = $wpdb->get_results("SELECT ID, post_title FROM $wpdb->posts WHERE post_status = 'publish' AND post_type='post' AND post_date_gmt < '$today' ORDER BY post_date DESC LIMIT 10")):
    
    ?>

    I tried this:

    AND post_category='photo_galleries

    but no worky!

  2. thebindlestick
    Member
    Posted 7 years ago #

    I know how to do the opposite like this:

    $myposts = get_posts('numberposts=10&offset=1&category=5');

    and display one category, but how do I display all BUT one?

    $myposts = get_posts('numberposts=10&offset=1&category!=5');

    doesn;t work with the (!) in there.

  3. Firas
    Member
    Posted 7 years ago #

    You need query_posts, not get_posts.

  4. thebindlestick
    Member
    Posted 7 years ago #

    Query_posts doesn't have an exclusion option, AFAIK.

  5. Firas
    Member
    Posted 7 years ago #

    Yes it does. The least you could do is look it up.

  6. thebindlestick
    Member
    Posted 7 years ago #

    On that page it says:

    " Excluding Multiple Categories

    Note: You cannot exclude more than one category with query_posts. Therefore you can use workarounds or plugins."

    Those work arounds are what I am looking for. I have tried conditional loops and they do not work. I am not looking for an if_home function, this is for my sidebar, to only display titles, not the entire post.

  7. thebindlestick
    Member
    Posted 7 years ago #

    How about this:

    <?php query_posts('cat=-5'); ?>
    
    <?php if (have_posts()) : ?>
    
    <?php while (have_posts()) : the_post(); ?>
    
    <div class="rss" id="post-<?php the_ID(); ?>">
    
    <h1><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h1>
    
    </div>
    
    <?php endwhile; ?>
    
    <?php endif; ?>

    That displays the correct list of posts in my sidebar but they all link to the post displayed on the main page, it is like the to query_posts interfere with eachother, is this the case?

Topic Closed

This topic has been closed to new replies.

About this Topic