0

I really need help for this problem in wordpress about displaying and filtering datas. I have html in this form, so first i have two drop down list(DDL), which should be populated related to the date of the posts. First DDL it should contains all the years of the posts, and second DDL should contains all the months of the posts.

<select name="month">
    <option value="2001"> 2001</option>                                                                        
    <option value="2002"> 2002</option>                                                                        
    <option value="2003"> 2003</option>                                                                      
    <option value="2004"> 2004</option>                                                                          
    <option value="2005"> 2005</option>                                                                      
    <option value="2006"> 2006</option>                                                                        
    <option value="2008"> 2008</option>                                                                            
   <option value="2000"> 2000</option>                                             
</select>



<select name="month">
        <option value="January"> 01 </option>                                                                        
        <option value="Feburary"> 02 </option>                                                                        
        <option value="March"> 03 </option>                                                                      
        <option value="April"> 04 </option>                                                                          
        <option value="June"> 06 </option>                                                                      
        <option value="July"> 07 </option>                                                                        
        <option value="August"> 08 </option>                                                                            
       <option value="December"> 12 </option>                                             
    </select>

Now I need to display the posts by selecting in DDL, month and year. I'm using while loop to display all the posts. Something like this:

<?php
    $posts_per_row = 3;
    $posts_per_page = 6;
    $category_name = 'music';
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args = array(
         'category_name' => $category_name,
         'posts_per_page' => $posts_per_page,
         'paged' => $paged,
    );
    query_posts($args);
    if (have_posts()) {
       while (have_posts()) {
       the_post();
       ....
    }
} else {
    ....                    
}
?>          

The template also has the pagination function in functions.php

ah92
  • 307
  • 5
  • 20

2 Answers2

1

You can display posts based on the posted month/year by using the year and monthnum arguments for query_posts().

// you have "month" in your HTML instead of "year"
$year = $_POST['year'];   
$month = $_POST['month']; 
$args = array(
     'category_name' => $category_name,
     'posts_per_page' => $posts_per_page,
     'year' => $year,
     'monthnum' => $month,
     'paged' => $paged,
);
doublesharp
  • 26,888
  • 6
  • 52
  • 73
  • Thank you for your solution, it is working but for the years earlier than 2014, POST method it is redirects me to the homepage even that i have posts in those years. I don't know where is the problem with this now !!!! – ah92 Jul 19 '14 at 23:20
0

You can use year and monthnum attribute in query arguments.

Like this:

$posts_per_row = 3;
$posts_per_page = 6;
$category_name = 'music';
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
     'category_name' => $category_name,
     'posts_per_page' => $posts_per_page,
     'paged' => $paged
);
if(isset($_POST['year'])){
   $args['year'] = $_POST['year'];
}
if(isset($_POST['month'])){
   $args['monthnum']= $_POST['month'];
}

query_posts($args);
if (have_posts()) {
   while (have_posts()) {
   the_post();
   ....
}

And your values in select "month" change to:

<select name="month">
        <option value="01"> 01 </option>                                                                        
        <option value="02"> 02 </option>                                                                                                                                            
         ...                                                                           
        <option value="12"> 12 </option>                                             
</select>

More info here: http://codex.wordpress.org/Function_Reference/query_posts

DzeryCZ
  • 370
  • 3
  • 17
  • Thank you for your solution, it is working but for the years earlier than 2014, POST method it is redirects me to the homepage even that i have posts in those years. I don't know where is the problem with this now !!!! – ah92 Jul 19 '14 at 23:21