1

I had posted this earlier on Stack Overflow, but couldn't get a positive result. I thought I should do this again.

<?php require_once 'news/wp-config.php';
$howMany = 0;
$query ="SELECT `ID`, `post_title`,'post_category', `guid`,SUBSTRING_INDEX(`post_content`, ' ', 100) AS `post_excerpt` FROM $wpdb->posts WHERE `post_status`= \"publish\" AND `post_type` = \"post\" AND post_category != \"1\" ";
$posts = $wpdb->get_results($query);
$posts = array_reverse($posts);
foreach($posts as $post)
{
    if($howmany<10)
    {
        $link = $post->guid;
        echo "<li><a target='_blank' href='$link'>$post->post_title</a></li>";
        $howmany++;
    }   

}                   
?>

I want the above code not to display posts from Category 1. I think I have done everything right, but still I can't get the result that I want.

One more thing is if I even explicitly ask the query to display the posts from category 3 or 4, it doesn't happen. It ends up displaying posts from all categories.

3
  • is your category id of char type? i'd imagine it should be some sort of int. Commented Sep 6, 2009 at 16:43
  • and you sholdn't have done this again. It would be better if you'd update your original question with the attempt to solve it. Commented Sep 6, 2009 at 16:46
  • i am sorry abt this. but it wasnt getting the proper response. i am really stuck on this. and the original post was made around a month ago... Commented Sep 6, 2009 at 16:50

2 Answers 2

2

You should use the query_posts() function. If not, at least get rid of that $howMany variable and instead append "LIMIT 10" to your sql query.

Here is an example of getting a category prepped for the loop:

<?php
     // Borrowed heavily from link above...
     $categoryvariable=1; // assign the variable as current category
     $numposts = 10;
     // Set up the query
     $query= 'cat=' . $categoryvariable. '&orderby=date&order=ASC&showposts='.$numposts;      
     query_posts($query); // run the query

//The Loop
if ( have_posts() ) : while ( have_posts() ) : the_post();
     // Do all your echo stuff here
endwhile; else:

endif;

//Reset Query
wp_reset_query();

?>
Sign up to request clarification or add additional context in comments.

Comments

1

I would second using the wordpress functions.

Here's a great resource http://codex.wordpress.org/Function_Reference/query_posts

The correct syntax would be query_posts('category__not_in=1');

I think you said you didn't want it in category 1.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.