Cannot figured it out… I need to count how many posts there are for each taxonomy term form each month.
Leave a Reply
You must be logged in to post a comment.
Cannot figured it out… I need to count how many posts there are for each taxonomy term form each month.
You must be logged in to post a comment.
This is a long answer and there is a lot of code, please note that it is all not tested.
My solution use a function that create a multidimensional array where key are year, month, term id and the value are the term count.
The function also accept as argument an array of args to pass to WP_Query, limiting the term counting to desidered posts, so you can count terms of a taxonomy in a specific period using WP_Query time options or only for posts that respond to another taxonomy query using tax_query options, and so on.
Data ireturned by function are very rich and can be manipulated in a lot of ways, as example I will post a function that return the counting for a specific term in a specific month.
Anyway, here you are:
The output of this function is an array which an inner item is something like:
that is: in the July 2013 the term with ID 132 was used 5 times
Note that this assertion is valid ‘in general’ (for all posts) only if second argument of function is empty (as is by default) otherwise is valid only for posts responding to the query args array passed as second param to function.
Now, you can use the array generated by this function with this other one:
The output of this function is a number, that is the counting for a given term id in a given month for a given query posts.
Use it is a lot easier than explain:
Following code shows how many times category with id ‘3’ is used in posts of current month:
As I previous said, the data generated by the
set_cont_taxonomy_term_for_months
are very rich, as example, following code shows tag counting in all the months of current year:If you have a lot of posts, doing 3 nested foreach cycles will slow down the script, so try to use the function
set_cont_taxonomy_term_for_months
passing $query_args as selective as possible and, if you can, write functions to return desired informations without perform cycles, just like I do with withget_month_term_count
function.Hope it helps.
Edit:
I have changed the 3 nested loops in my last example, now it should works.
One method you might want to research is
wp_get_archives
http://codex.wordpress.org/Function_Reference/wp_get_archives
You might also be able to use
the_date()
to sort out posts in a given month.http://codex.wordpress.org/Function_Reference/the_date
I would try to find a function that can store results in an array, and then use
count()
on the array to learn it’s contents…Good luck!