WordPress.org

Ready to get started?Download WordPress

Forums

SQL query for one category - problem (2 posts)

  1. mathias_h
    Member
    Posted 8 years ago #

    I'm using the CleanArchives plug-in which lists all posts in a nice way. I want to filter out all categories except one. This is what the original query looks like:

    $arcresults = $wpdb->get_results("SELECT DISTINCT YEAR(post_date) AS year, MONTH(post_date) AS month, count(ID) as posts FROM " . $wpdb->posts . " WHERE post_date <'" . $now . "' AND post_status='publish' AND post_password='' GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC");

    I've modified the query by selecting from the post2cat table as well and by selecting posts where ID in "posts" matches "post_id" in post2cat. Then I choose only articles where the category_id = '1'. The weird thing is that I articles from cat1 *and* two show up, but not three. If I choose articles from category two, one *and* two show up but not three. If I choose articles from cat 3, only cat 3 articles turn up. I really can't understand why. This is what my query looks like. I'm pretty lousy at SQL so maybe it's totally weird:

    $arcresults = $wpdb->get_results("SELECT DISTINCT YEAR(post_date) AS year, MONTH(post_date) AS month, count(ID) as posts FROM " . $wpdb->posts .",". $wpdb->post2cat . " WHERE post_date <'" . $now . "' AND post_status='publish' AND post_password='' AND ID=post_id AND category_id = '1' GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC");

  2. mathias_h
    Member
    Posted 8 years ago #

    Solved it. The same line where in the script twice, for some reason it all went weird if I only changed it in one place.

Topic Closed

This topic has been closed to new replies.

About this Topic