views:

115

answers:

5

i have been presented with this code. the code displays the title headings of the latest 10 wordpress posts from the database. what i need to do is to eliminate a particular category from this. can anyone please help?

<?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\" ";
        $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++;
          } 

        }


        ?>
A: 

Determine the category you don't need and add an extra AND to your WHERE clause:

AND post_category != 3
meder
that doesnt seem to work.
amit
Er - did you try that literally or did you substitute 3 with 'Noticeboard' ?WHERE `post_status`= \"publish\" AND `post_type` = \"post\" AND post_category != \"Noticeboard\""
meder
of course i did :)
amit
A: 

You can do it either of 2 places. The most efficient is to do it in the query:

 $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!= 'unwanted string' ";

The other place to do it, if for some reason you need all the results, but you want to use the unwanted category somewhere else, is when you retrieve the results:

if($howmany<10 && post['post_category']!='unwanted string') {
dnagirl
'Noticeboard' is the name of the category i want to eliminate. but if i write that, it dispalys a parse error.
amit
A: 

'Noticeboard' is the name of the category i want to eliminate. but if i write that, it dispalys a parse error.

Because you must insert the category ID in the query, not the category name.
To get that, just watch in the database wp_categories table.

Some links about wordpress database:
http://blog.kapish.co.in/2008/01/18/wordpress-database-schema/
http://wpbits.wordpress.com/2007/08/08/a-look-inside-the-wordpress-database/

Anyway, I think it's more hard than this. Look at post2cat table. So, you have to do a subquery.

sydarex
i tried category-id as well. the category-id is 1. it doesnt do the trick.
amit
A: 

I am not sure which WP version you are using, so this 'answer' has a few caveats.

Caveat 1 : This is not a complete answer, more like a pointer

Caveat 2 : The query below works with WP 2.8.x so YMMV

The query below shows how to link posts back to their categories. You can use the NOT IN mySQL operator to exclude the category you do not want by its ID

SELECT
    wp_posts.*
FROM
    wp_posts
INNER JOIN
    wp_term_relationships
ON
    (wp_posts.ID = wp_term_relationships.object_id)
INNER JOIN
    wp_term_taxonomy
ON
    (wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id)
WHERE
    wp_term_taxonomy.taxonomy = 'category'
AND
    wp_term_taxonomy.term_id NOT IN ('3')
AND
    wp_posts.post_type = 'post'
AND
    (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'future')
GROUP BY
    wp_posts.ID
ORDER BY
    wp_posts.post_date
DESC

The line breaks and indenting are idiosyncratic, but (hopefully) make it easier for you to see what is going on with this query.

I hope this helps.

starepod
A: 

Or, you could use a second loop, something like this :

<div>
    <h3>Fresh Posts</h3>
    <ul>
    <?php
     $my_query = new WP_Query("cat=-3&order=DESC&posts_per_page=10");

     echo "<pre>"; print_r($my_query->query_vars); echo "</pre>"; // shows the variables used to parse the query
     echo "<code style='width: 175px;'>"; print_r($my_query->request); echo "</code>"; // shows the parsed query

    while ($my_query->have_posts()) : $my_query->the_post(); //standard loop stuff
    $do_not_duplicate[] = $post->ID;?>

     <li id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>

    <?php endwhile; ?>
    </ul>
</div>

once again WP 2.8.x. Lots of good info here : WordPress loop documentation

starepod