WordPress.org

Ready to get started?Download WordPress

Forums

Filtering by Date Range Against a Global Variable (7 posts)

  1. saracup
    Member
    Posted 1 year ago #

    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 title

    etc.

    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:

    http://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!

  2. Ron Rennick
    MultiSite Guru
    Posted 1 year ago #

    Instead of

    function uvasomnews_filter_where( $where = 'post_type=post&orderby=title&order=asc') {

    $uvasomnews_issue_query = new WP_Query( $query_string );

    use

    function uvasomnews_filter_where( $where ) {

    $uvasomnews_issue_query = new WP_Query( 'post_type=post&orderby=title&order=asc' );

  3. saracup
    Member
    Posted 1 year ago #

    Ron Rennick to the rescue! The query now works. Now for the categories. Will post when that is resolved -- have to go to the office!

  4. saracup
    Member
    Posted 1 year ago #

    Okay, working on nesting the now-functional WP_Query() inside their respective categories. I'm trying to pass the variable from the array for that particular category ID so I can further narrow down each WP_Query. So far, no luck. I've put the words "ADDITIONAL ARGUMENTS HERE??" where I think I have to put the variable for the category ID. The category ID is parsing when I output the title for the category. Although the WP_Query is nested inside the "foreach" it's not respecting the category and is returning all articles.

    http://administration.emedicine.virginia.edu/medicinematters/test-email/

    The custom WP_Query function:

    function uvasomnews_filter_where( $where = '') {
    	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( 'post_type=post&orderby=title&order=asc' );
    remove_filter( 'posts_where', 'uvasomnews_filter_where' );

    The output file (would love to put this separately in a functions file but am now testing):

    <?php
    $uvasomnews_categories_args=array(
      'orderby' => 'name',
      'order' => 'ASC',
      'cat' => '-'.$uvasom_news_main_feature
      );
    $uvasomnews_categories=get_categories($uvasomnews_categories_args);
      foreach($uvasomnews_categories as $uvasomnews_category) {
        echo '<h2><a href="' . get_category_link( $uvasomnews_category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $uvasomnews_category->name ) . '" ' . '>' . $uvasomnews_category->name.'</a>cat='. $uvasomnews_category->term_id.' </h2> ';?></h4>
        <?php new WP_Query(ADDITIONAL ARGUMENTS HERE??); while($uvasomnews_issue_query->have_posts()) : $uvasomnews_issue_query->the_post();?>
                <p style="font-family:Georgia, 'Times New Roman', Times, serif;font-weight:normal;font-size:13px;margin:0px;padding:5px 0 0;"><a style="color:#333333;text-decoration:none;" href="<?php the_permalink() ?>" rel="bookmark">
                          <?php the_title(); ?>
                        </a></p>
                      <?php endwhile; //end wp_query
    
    				  } //end category foreach ?>
  5. saracup
    Member
    Posted 1 year ago #

    Oh, by the way, the inline font tags are due to the fact that this outputs to a table-based layout for html email.

    At any rate, does anyone have any clue as to why my query is not respecting the parent category when I have it nested within the "foreach" construct? I'm sure this is major stupid, but any help would be most appreciated :)

  6. Ron Rennick
    MultiSite Guru
    Posted 1 year ago #

    <?php new WP_Query(ADDITIONAL ARGUMENTS HERE??); while($uvasomnews_issue_query->have_posts()) : $uvasomnews_issue_query->the_post();?>

    should be

    <?php $uvasomnews_issue_query = new WP_Query('post_type=post&orderby=title&order=asc&cat=' . $uvasomnews_category->term_id); while($uvasomnews_issue_query->have_posts()) : $uvasomnews_issue_query->the_post();?>

  7. saracup
    Member
    Posted 1 year ago #

    Ron, thanks again. It's working with the nesting, but the arguments from the original function, which filtered by date range, are now lost. I'm thinking there's an easier way to get there -- maybe by having folks categorize by issue number.

    Thank you for everything!!!!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.