0

Questions is a duplicate of this one: Group Posts by First Letter of Title

However, I was unsure whether I could ask on that, or I had to start a new question, so I did the latter ;)

I implemented the solution that @helgatheviking suggested in 2014 and it works/worked brilliantly, however - I have to do another list for 2015, I have tried to figure out how to separate the categories into two different lists but can't do it.... I have a category called 2014 & 2015

I tried to alter this line:

$posts = get_posts(array('numberposts' => -1) );

by adding 'category' => x but that didn't work as I thought it might...

Any help would be fantastic and thanks in advance.

4
  • 1
    To clarify, you need A-Z categories for 2014 and then, entirely separate from that, you need A-Z categories for 2015? – mor7ifer Jan 27 '15 at 19:00
  • 2
    that didn't work as I thought it might. What did you expect the output to be and what did you actually get. This info can be handy to someone that will answer your question :-) – Pieter Goosen Jan 27 '15 at 19:04
  • Sorry for my delay in replying, I was on a photo shoot... m0r7if3r - yes, two entirely separate A-Z's... PieterGoosen I thought somehow it would've filtered the categories into the correct taxonomies - to be honest, I still don't fully understand taxonomies, the solution that helgatheviking was the best solution I could find at the time and worked perfectly... – Andy Jan 27 '15 at 22:16
  • @Andy In that case, see my answer below, it should at least get you going in the right direction. – mor7ifer Jan 28 '15 at 17:42
0

I would use WP_Query for this, as I am more comfortable with it.

There are two solutions to this that occur to me. If you're not heartset on the categories, then WP_Query has good support for date parameters, to include a year parameter. A query by year would look something like this:

$wpse176213_query = new WP_Query( array(
    'nopaging'  => true,            // return all posts
    'year'      => $your_year_var   // Must by 4 digit
) );

If you're heartset on using your categories, WP_Query also supports that. That would look something like this:

$wpse166213_query = new WP_Query( array(
    'nopaging'  => true,            // return all posts
    'tax_query' => array(
        'taxonomy'  => 'category',
        'field'     => 'slug',
        // Whatever the slug for the year category is
        'terms'     => $your_year_slug
    )
) );

Note that, if you like, you can do a simpler query without using tax_query, but I tend to favor tax_query as it allows you to extend the code with less additional effort, if you ever run into that. Also note that these queries are not complete, I strongly recommend you read through the (long) list of available parameters and make sure you set them up correctly. See the codex entry for WP_Query.

5
  • As above, I came in late after a photoshoot, I will definitely try this first thing in the morning as well as looking at the codex with a fresh set of eyes, I really do appreciate your time. thanks again :) – Andy Jan 27 '15 at 22:19
  • @Andy If it throws an error, LMK and I'll update the code to reflect a more accurate answer as relevant. – mor7ifer Jan 28 '15 at 17:43
  • Hey @m0r7if3r - I've had a hectic couple of days and not free to try this till Saturday now - I promise I will get back to you though. – Andy Jan 29 '15 at 18:56
  • Sure, just post a comment on the answer or something so that I get a notification. – mor7ifer Jan 29 '15 at 19:43
  • apologies for how long it took to get back to you (had a massive project on the go), it worked a treat, so thank you massively. – Andy Jun 23 '15 at 15:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.