• I feel like I’ve exhausted all searches on this subject & still not found one definitive answer! I have a client who needs to display a list of archives by month & with count within a menu. However, because they have also used posts to categorize client case study pages, they only wish to display a list of posts in the menu from one category & by month & with count;

    January 2012 (7)
    December 2011 (2)
    etc..

    I have tried pretty much every suggestion so far on this forum on this subject but with no luck s yet. This article did manage to get the list itself to appear correct but when the links were clicked, archives from all categories appeared again! I imagine that’s down to WP not being able to differentiate between archive categories when also dividing into months.

    I was wondering whether if the archive.php page within my theme could be made to filter only 1 category’s results?

    Anyways, any advise or suggestions would be most welcome!

    Many thanks in advance.

Viewing 15 replies - 1 through 15 (of 41 total)
  • Thread Starter deeve

    (@deeve)

    Thanks iamgarrett, I did but couldn’t see where to specify the output to be by month & with count..

    Hmm…

    You could set up a loop query (WP_Query) that looks for all the posts of a category you want. Then If that query has posts you could set up a while loop that looks at each post’s publish month and if it matches a counter you set up (the counter standing for the number of the month) it would spit it out for that month.

    Thread Starter deeve

    (@deeve)

    I did try this in my menu:

    <ul>
             <?php
    	 query_posts('cat=7');
             while ( have_posts() ) : the_post();
             get_archives('monthly', '5', 'html', '', '', TRUE);
             endwhile;
    	 wp_reset_query();
    	 ?>
    </ul>

    in conjunction with this in functions.php:

    add_filter( 'getarchives_where', 'customarchives_where' );
    add_filter( 'getarchives_join', 'customarchives_join' );
    
    function customarchives_join( $x ) {
    	global $wpdb;
    	return $x . " INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)";
    }
    function customarchives_where( $x ) {
    	global $wpdb;
    	//$exclude = '1'; // category id to exclude
    	//return $x . " AND $wpdb->term_taxonomy.taxonomy = 'category' AND $wpdb->term_taxonomy.term_id NOT IN ($exclude)";
    	$include = '7'; // category id to include
    	return $x . " AND $wpdb->term_taxonomy.taxonomy = 'category' AND $wpdb->term_taxonomy.term_id IN ($include)";
    }

    but it just repeats the query 10 times & with the same problem on the links as stated in my first post.

    I must admit to loving the efficiency of the function:
    <?php get_archives('monthly', '5', 'html', '', '', TRUE); ?>
    but again, this will return all archives!

    pseudo code that will do an approximation this, will require some finessing by you:
    https://pastebin.com/iM4YFcAb

    That isn’t complete but is all I really have time to write at the moment. Hopefully it gets you pointed in the right direction.

    Thread Starter deeve

    (@deeve)

    @iamgarrett: many thanks for your time – I will look further into that.

    I’ll try and look back at this later and see if you haven’t come up with it or been helped otherwise. If not, I’ll see if I can finish that example.

    I think this might have to be taken care of in three steps.

    1. The first step is getting a list of years and months with a count of how many posts were in that category. I think I’ve written something a little more fleshed out than above HERE that should work for those purposes.

    2. You should turn that data into links that will send the year, month, and category to a new template in your theme. This will be the page that lists posts for a month and category (ex: cat-month.php ).

    3. The third step would be to set up a template in your theme that calls a function ( best defined in your theme’s functions.php ) that uses the data sent to write a query (very similar to the query I wrote above) and lists the relevant posts for you.

    I hope this helps you.

    Ugh line 21 of that latest pastebin should have a wp_reset_query(); after the closing brace.

    You know what? I should have proofread that a couple more times, here is a better version of that. Use this. https://pastebin.com/eytSdiip

    Thread Starter deeve

    (@deeve)

    Hi iamgarrett,
    I gave that a try as it is & by changing year limit to 2000 & category to 7 & got the following output:

    201220112010200920082007200620052004200320022001

    Awesome! It looks like you’re just getting a bunch of year numbers. That tells me that everything is going fine in the first while statement. Something is going wrong somewhere in there… let’s try adding some testing echo lines in there to see where it is fouling up.

    Make a new line under line 6. The new line will say echo 'test 1';

    Hopefully that will output something like:

    2012test12011test12010test1….

    If so, that limits the problem to the db query we wrote. Double check that the category you want to target actually has an ID # of 7.

    Thread Starter deeve

    (@deeve)

    ..not quite. It outputs exactly like this:

    2012test 1test
    1test 1test 1test
    1test 1test 1test
    1test 1test 1test
    1test
    120112010200920082007200620052004200320022001

    NB: I do have it placed within a fixd width div but the last line exceeds that width so can’t imagine it has any bearing on this.

    I have to get some shut eye now but will look back into this again tomorrow. I much appreciate your guidance so far ?? TTFN

Viewing 15 replies - 1 through 15 (of 41 total)
  • The topic ‘List of posts from specific category by month & with count?’ is closed to new replies.