Filtering by Date Range Against a Global Variable
-
In this instance, the client would like to display a list of all posts within a given set of categories for a given date range, output by category. The final listing should read something like this:
Category 1
Post title
Post title
Category 2
Post title
Category 3
Post title
Post titleetc.
I have a global variable for the date ranges called “$uvasomnews_IssueStart” and “$uvasomnews_issueEnd”. My date filter is as follows:
function uvasomnews_filter_where( $where = 'post_type=post&orderby=title&order=asc') { global $uvasomnews_issueStart; global $uvasomnews_issueEnd; $where .= " AND post_date >= '".$uvasomnews_issueStart."' AND post_date <= '".$uvasomnews_issueEnd."'"; return $where; } add_filter( 'posts_where', 'uvasomnews_filter_where' ); $uvasomnews_issue_query = new WP_Query( $query_string ); remove_filter( 'posts_where', 'uvasomnews_filter_where' );
My code retrieving the posts is as follows:
new WP_Query(); while($uvasomnews_issue_query->have_posts()) : $uvasomnews_issue_query->the_post();?> <p><a>" rel="bookmark"><?php the_title(); ?></a></p> <?php endwhile; ?>
[Please post code between backticks or use the code button. Your posted code may now have been permanently damaged by the forum’s parser.]
MY QUESTIONS:
1) There are about eight posts within the date range. The above filter is returning only a PAGE that was authored in that date range, despite the fact that “post_type=post” is specified. When I change the date range to a time that excludes that particular page, it returns nothing, which tells me that it’s successfully evaluating the range. In addition, I know it’s successfully reading the global variable because I can output it to the browser. What am I doing wrong? Below is the link with the list of the posts to the right, returning the single page rather than the 8 or so posts within the date range. It is worth noting that the page it is retrieving in the list to the right is the current page:
https://administration.emedicine.virginia.edu/medicinematters/test-email/
2) The second question I have is how to organize these into categories for the output. My understanding is that you cannot retrieve the categories prior to running WP_query. So, that would make me assume that I have to query all the posts in the range and THEN get their respective categories, sort, and display. This is a circular reasoning that has me completely flummoxed. My global variable for the categories involves excluding a single category (cat=-$uvasom_news_main_feature OR exclude => uvasom_news_main_feature).
Can anyone help me with this as I’ve been struggling for, embarrassingly, two days with this. I have never posted something of this complexity to this support forum, and have tried my best to avoid it ?? Many, many thanks!
- The topic ‘Filtering by Date Range Against a Global Variable’ is closed to new replies.