• Resolved Peter Monte

    (@stevestall)


    Is it possible to achieve this:

    ^ 2012(22)
    JANUARY(18)
    FEBRUARY(4)
    > 2011(123)
    > 2010(984)
    > 2009(557)

    with a single category ?

    Thanks

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter Peter Monte

    (@stevestall)

    Found this code:

    $results = $wpdb->get_results
    (
    "SELECT YEAR(post_date) AS 'year', MONTH(post_date) AS 'month', count(ID) as posts
    FROM $wpdb->posts
    WHERE post_type = 'post' 						AND post_status = 'publish'
    GROUP BY YEAR(post_date), MONTH(post_date) 					ORDER BY post_date DESC"
    );

    problem is that it is not specific for a single category

    any help?
    PM

    Thread Starter Peter Monte

    (@stevestall)

    Found this link that solved my problem:

    https://wordpress.stackexchange.com/questions/10988/disallow-categories-from-this-mysql-query

    $tax_query = array ( array( 'taxonomy' => 'category', 'terms' => array ( 8 ), 'operator' => 'IN' ) );
    
    $clauses = get_tax_sql ( $tax_query, $wpdb->posts, 'ID' );
    
    $results = $wpdb->get_results
    (
       "SELECT YEAR(post_date) AS 'year', MONTH(post_date) AS 'month', count(ID) as posts
       FROM $wpdb->posts {$clauses['join']}
       WHERE post_type = 'post'
       AND post_status = 'publish'
       {$clauses['where']}
       GROUP BY YEAR(post_date), MONTH(post_date)
       ORDER BY post_date DESC"
    );

    Basically it provides the mySQL query for the JOIN part and the WHERE statements. In my case I change ‘operator’ => ‘NOT IN’ to ‘operator’ => ‘IN’

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘archive by year and month’ is closed to new replies.