• Hi,

    One of the features that I’m using this plugins for is to search posts by month/year. So I have already set it up and added coded, found from previous posts here in the support forum, that helps achieve just that. Now, I can search posts by date & year, for example October 2015, if I wanted. I have also made it so that you can click on multiple dates, but the search results don’t seem to show all of those multiple dates. For example, if I select October 2016 and September 2016, the only post from September 2016 are shown.

    Any advise?

    Thanks, and I do really enjoy this plugin!

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Author TC.K

    (@wp_dummy)

    What did you do?
    Can you shows your codes?

    Thread Starter imthatguydavid

    (@imthatguydavid)

    Sure, here’s the code I have.

    function insert_yearmon_input($attr){
    
    if($attr['id'] == '105'){
    global $wpdb, $wp_locale;
    
    	
    	$query = "SELECT YEAR(post_date) AS <code>year</code>, MONTH(post_date) AS <code>month</code>, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC";
    		$results = $wpdb->get_results( $query );
    
    		
    	$html = '<div class="uwpqsf_class" id="tax-check-2"><span class="taxolabel-2">Date</span>';
    
    		
    	foreach($results as $result){
    			
    		$html .= '<label><input name="yearmonth" value="'.$result->year.sprintf("%02d", $result->month).'" type="checkbox" class="datep">'.sprintf( __( '%1$s %2$d' ), $wp_locale->get_month( $result->month ), $result->year ).'</label>';
    	    }
    
    	    $html .='</div>';
    	    echo $html;
    	}
    
    }
    Plugin Author TC.K

    (@wp_dummy)

    No, you can’t have multiple dates included for the search.
    AS you can see in the date query in wp_query class. You can’t have multiple date in the query.
    Maybe you can change it to include the range of date instead, eg from October 2015 to January 2016.

    Thread Starter imthatguydavid

    (@imthatguydavid)

    So currently, it’s set up so users can click on the date, and a check mark is ticked. The users can click on multiple check marks.

    Is it possible to have the query to use the checked dates as the range?

    Thread Starter imthatguydavid

    (@imthatguydavid)

    I’m not sure if that first batch of codes is what’s responsible for the query search. here’s all of my code that deals with UWPQSF in my functions.php. Sorry for the long text a code. I’m not to comfortable with wordpress php and so any help would be greatly appreciated.

    // ** ADD A DATE SEARCH FILTER TO THE UWPQSF PLUGIN ** //
    add_filter('uwpqsf_query_args','insert_yearmon_to_query','',3);
    
    // Add the 'YearMonth' date parameter to the query arguments
    function insert_yearmon_to_query($args,$id,$getdata){
    	$argsM = array(
    		'm'  =>  $getdata['yearmonth'],
    	);
    
      // Merge new array with existing array
      return array_merge($args, $argsM);
    
    }
    
    // Insert month and corresponding year into UWPQSF#105
    add_action('uwpqsf_form_bottom','insert_yearmon_input');
    
    function insert_yearmon_input($attr){
    
    	if($attr['id'] == '105'){
    	global $wpdb, $wp_locale;
    
    		// 1) Search WP database for posts by Year, Month, and Post Count
    		$query = "SELECT YEAR(post_date) AS <code>year</code>, MONTH(post_date) AS <code>month</code>, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC";
    		$results = $wpdb->get_results( $query );
    
    		// 2) Add a container for the following YearMonth search filter
    		$html = '<div class="uwpqsf_class" id="tax-check-2"><span class="taxolabel-2">Date</span>';
    
    		// Foreach loop for the results from Step 1.
    		foreach($results as $result){
    			// Add an input field and in the value echo the data for the year and month (if the month is less than 2 digits add a 0 to the front)
    			// As for the label, print the month and year for the corresponding result
    			$html .= '<label><input name="yearmonth" value="'.$result->year.sprintf("%02d", $result->month).'" type="checkbox" class="datep">'.sprintf( __( '%1$s %2$d' ), $wp_locale->get_month( $result->month ), $result->year ).'</label>';
    	    }
    
    	    $html .='</div>';
    	    echo $html;
    	}
    
    }
    // CUSTOMIZE THE HTML SETUP OF THE SEARCH RESULT PAGE
    add_filter('uwpqsf_result_tempt', 'customize_output', '', 4);
    
    function customize_output( $results, $arg, $id, $getdata ){
    	// The Query
    	$apiclass = new uwpqsfprocess();
    
    	// Set a variable for each pagination page number
    	$pagenumber = isset($_POST['pagenum']) ? $_POST['pagenum'] : null;
    
    	$query = new WP_Query( $arg );
    	// Set a variable to keep track of what post the loop is on
    	$i = 0;
    
    	ob_start();	$result = '';
    
    	// The Loop
    	if ( $query->have_posts() ) {
    		// Wrap all the search result in a container called #ajax-sr-container
    		$html = '<div id="ajax-sr-container">';
    		
    		while ( $query->have_posts() ) {
    		$query->the_post();global $post, $paged, $numpages;
    
    		// If the user is on the first page of the search results...
    		if(1 == $pagenumber){
    			// Style the first post in a specific way
    			if($i == 0){
    				$html .= '<div class="ajax-sr latest-post" id="item-'.$i.'">';
    				$html .= '<div id="latest-label"><span>LATEST POST</span></div>';
    				$html .= '<div class="ajax-fimg">'.get_the_post_thumbnail($post_id, 'full').'</div>';
    				$html .= '<div class="background"></div>';
    				$html .= '<div class="slider-txt-content">';
    				$html .= '<a class="title_link" href="'.get_permalink().'" title="Read full post">'.get_the_title().'</a>';
                    $html .= '<p class="author">'.get_the_author().'</p>';
    				$html .= '<p class="post_date">'.get_the_date().'</p>';
    				$html .= '<p class="excerpt">'.get_the_excerpt().'</p>';
    				$html .= '</div>';
    				$html .= '</div>';
    				$html .= '<div id="nf-barrier"></div>';
    			// Style the 1st, 2nd, and 3rd posts a different way
    			} else if ($i <= 3){
    				$html .= '<div class="ajax-sr ajax-posts" id="item-'.$i.'">';
    				$html .= '<div class="ajax-fimg">'.get_the_post_thumbnail($post_id, 'full').'</div>';
    				$html .= '<div class="result-content">';
    				$html .= '<a class="title_link" href="'.get_permalink().'" title="Read full post">'.get_the_title().'</a>';
                    $html .= '<p class="author">'.get_the_author().'</p>';
    				$html .= '<p class="post_date">'.get_the_date().'</p>';
    				$html .= '<p class="excerpt">'.get_the_excerpt().'</p>';
    				$html .= '</div>';
    				$html .= '</div>';
    			// Hide all search results after the fourth search result
    			} else if ( $i > 3){
    				$html .= '<div class="ajax-sr ajax-posts hidden-sr" id="item-'.$i.'">';
    				$html .= '<div class="ajax-fimg">'.get_the_post_thumbnail($post_id, 'full').'</div>';
    				$html .= '<div class="result-content">';
    				$html .= '<a class="title_link" href="'.get_permalink().'" title="Read full post">'.get_the_title().'</a>';
                    $html .= '<p class="author">'.get_the_author().'</p>';
    				$html .= '<p class="post_date">'.get_the_date().'</p>';
    				$html .= '<p class="excerpt">'.get_the_excerpt().'</p>';
    				$html .= '</div>';
    				$html .= '</div>';
    			} 		
    			
    			$i++;
    		
    		// If the user is NOT on the first page of search results
    		} else {
    			if ($i <= 3){
    				$html .= '<div class="ajax-sr ajax-posts" id="item-'.$i.'">';
    				$html .= '<div class="ajax-fimg">'.get_the_post_thumbnail($post_id, 'full').'</div>';
    				$html .= '<div class="result-content">';
    				$html .= '<a class="title_link" href="'.get_permalink().'" title="Read full post">'.get_the_title().'</a>';
    	            $html .= '<p class="author">'.get_the_author().'</p>';
    				$html .= '<p class="post_date">'.get_the_date().'</p>';
    				$html .= '<p class="excerpt">'.get_the_excerpt().'</p>';
    				$html .= '</div>';
    				$html .= '</div>';
    			// Hide all search results after the fourth search result
    			} else if ( $i < 8 && $i > 3){
    				$html .= '<div class="ajax-sr ajax-posts hidden-sr" id="item-'.$i.'">';
    				$html .= '<div class="ajax-fimg">'.get_the_post_thumbnail($post_id, 'full').'</div>';
    				$html .= '<div class="result-content">';
    				$html .= '<a class="title_link" href="'.get_permalink().'" title="Read full post">'.get_the_title().'</a>';
    	            $html .= '<p class="author">'.get_the_author().'</p>';
    				$html .= '<p class="post_date">'.get_the_date().'</p>';
    				$html .= '<p class="excerpt">'.get_the_excerpt().'</p>';
    				$html .= '</div>';
    				$html .= '</div>';
    			} 
    
    			$i++;		
    		} //End of else condition
    
    	} //End of If loop		
    
    	// If there are more than four posts, add a load more button
    	if($i > 3){
    		$html .= '<div id="load-more"><img alt="Read from the LA84 Knowledge Center" src="/wp-content/uploads/2016/07/loadmore.png"></div>';
    	}
    
    	$html .= '</div>';
    	echo $html;
    
    	// Add a pagination to the bottom of the search results
    	echo  $apiclass->ajax_pagination($arg['paged'],$query->max_num_pages, 4, $id);
    
    	} else {
    
    		// If there aren't any posts...
    		$html2 =  '<div id="no-posts">No post found</div>';
    		echo $html2;
    	}
    	/* Restore original Post Data */
    	wp_reset_postdata();
    
    	$results = ob_get_clean();		
    	return $results;
    }
    Thread Starter imthatguydavid

    (@imthatguydavid)

    Also, if you want a better understanding of what I’m trying to do, maybe I can send you a link to the site to check it out.

    Plugin Author TC.K

    (@wp_dummy)

    Is it possible to have the query to use the checked dates as the range?

    How do we know which range of dates when user checked more than two dates? So, no, I don’t recommend using checkbox as range. It is confusing.

     // Add the 'YearMonth' date parameter to the query arguments
    function insert_yearmon_to_query($args,$id,$getdata){
    	$argsM = array(
    		'm'  =>  $getdata['yearmonth'],
    	);
    
      // Merge new array with existing array
      return array_merge($args, $argsM);
    
    }

    Here you got it wrong, You can clearly see that you are insert an array to the ‘m’ parameter. Because the $getdata['yearmonth'] is an array that coming from checkboxes.
    If you want to change it to range, you can start from this function and with a custom date_query parameter by referring the wp query Date Parameters documentation.

    Thread Starter imthatguydavid

    (@imthatguydavid)

    Hey,

    So the person did like the date range, and so we’re going to stick with the checkboxes. However, I’m trying to make it so that only one check is possible for the query. I thought that if I change
    return array_merge($args, $argsM); to return array_merge($argsM); it would make it so only one date would be queried, but it doesn’t seem to be working.

    I also changed the js so that only one date is checked at a time now, but it seems like the query doesn’t know that. Can you help me change it so only the checked date is the date that is being added to the query?

    I hope that makes sense. And you’ve been incredible helpful, I appreciate it a lot!

    Plugin Author TC.K

    (@wp_dummy)

    No, you can’t. If it is checkbox input, the backend will treat it as array. Thus you will not getting the correct result.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘searching posts by month and year’ is closed to new replies.