0

Hi I am trying to create a form that filters my posts by 3 requirements, Categories, Author and Date. The user should be able to select a requirement from each dropdown (or leave empty for all) and get an outcoem based on that, so for example be able to select HMTL(from categories), Joe Bloggs(from Author) and February 2017(from date) and get a page that show all posts in the HTMl, written by Joe Bloggs, published in Feb 2017.

So far I have some functions and a form, which when selecting a category and author (no date) appears to work and directs me to news/category/news/?user=1&date-dd&submit=filter which shows what I want.

However the date is where problems start. If I select the same category and author as previous but then select January 2017 I get the URL news/category/news/?user=1&date-dd=http%3A%2F%2Flocalhost%2Fbrandbiology%2Fnews%2F2017%2F01%2F&submit=filter which now doesnt work, it will show the category and author but no date and a weird url.

Secondaly if I just put <?php wp_get_archives( $args ); ?> on my page, it will display a list of dates within an <li> (not what I want but I did it for testing) and click January 2017 in that it will take me to news/2017/01/ which will display only posts in January (doesnt work with category IE news/category/blog/2017/01/ - shows 404).

My questions therefore is trying to create the right outcome that will display the posts based on the 3 requirements I want.

Thanks in Advance

Here is my Functions:

function get_cat_droplist(){
    $args = array(
        'show_option_all'    => 'All Categories',
        'show_option_none'   => '',
        'option_none_value'  => '-1',
        'orderby'            => 'ID',
        'order'              => 'ASC',
        'show_count'         => 1,
        'hide_empty'         => 1,
        'child_of'           => 0,
        'exclude'            => '',
        'include'            => '',
        'echo'               => 1,
        'selected'           => 0,
        'hierarchical'       => 0,
        'name'               => 'cat',
        'id'                 => '',
        'class'              => 'postform',
        'depth'              => 0,
        'tab_index'          => 0,
        'taxonomy'           => 'category',
        'hide_if_empty'      => true,
        'value_field'        => 'term_id',
    ); 
    $output = wp_dropdown_categories( $args );
}
function get_author_droplist(){
    $args = array(
        'show_option_all'         => 'All Author', // string
        'show_option_none'        => null, // string
        'hide_if_only_one_author' => true, // string
        'orderby'                 => 'display_name',
        'order'                   => 'ASC',
        'include'                 => null, // string
        'exclude'                 => null, // string
        'multi'                   => false,
        'show'                    => 'display_name',
        'echo'                    => true,
        'selected'                => false,
        'include_selected'        => false,
        'name'                    => 'user', // string
        'id'                      => null, // integer
        'class'                   => null, // string 
        'blog_id'                 => $GLOBALS['blog_id'],
        'who'                     => null // string
    );
    $output = wp_dropdown_users( $args );
}
function get_date_droplist(){
  $args = array(
      'type'            => 'monthly',
      'limit'           => '',
      'format'          => 'option', 
      'before'          => '',
      'after'           => '',
      'show_post_count' => true,
      'echo'            => 0,
      'order'           => 'DESC',
      'post_type'       => 'post'
  );
  $output = wp_get_archives( $args );
}

and my HTML/PHP

    <form action="<?php bloginfo('url'); ?>" method="get">
        <div>
          <span class="cat-list-header text-color-1">Filter posts:</span>
            <?php
              $select = get_cat_droplist();
              echo $select;
            ?>

            <?php
              $select = get_author_droplist();
              echo $select;
            ?>

            <select name="date-dd">
              <option value=""><?php echo esc_attr( __( 'Select Month' ) ); ?></option> 
              <?php wp_get_archives( array( 'type' => 'monthly', 'format' => 'option', 'show_post_count' => 1 ) ); ?>
            </select>

            <input type="submit" name="submit" value="filter" /> <!--CHANGE VALUE TO YOUR LIKING!-->
        </div>
    </form>
  • Why do you want a button? Do you need to select multiple options using checkbox? – Jack Johansson Feb 14 '17 at 14:44
  • Just drop the AJAX part and add the Submit button. – Max Yudin Feb 14 '17 at 14:46
  • @MaxYudin Yes that's what i thought. The rest is pretty easy to do. – Jack Johansson Feb 14 '17 at 14:48
  • Im quite new to wordpress and not sure how droppping the ajax and adding a submit button would work in terms if page setups – snookian Feb 14 '17 at 14:50
  • Can't find an example of it anywhere to look throiugh to get an understanding – snookian Feb 14 '17 at 14:52
1

1- To get a list of authors, you can use:

<?php wp_list_authors( ); ?>

This will return a list of authors and a link to their accounts and posts. To customize it using your desired arguments, take a look at Codex

2- To retrieve the categories, use:

<?php wp_list_categories(); ?>

This function will return a list of all categories as an HTML list. You can customize this too, here.

3- Finally, to filter the posts by date, you can use the method suggested in the link you provided, by using:

<?php wp_get_archives(); ?> and setting the arguments to monthly, or yearly. The guide has explained it pretty well.

Note: You don't need to use <options> selectors to make a drop down menu. These functions will return their results as HTML list, so you can style it just like a drop down menu. A full guide about CSS drop downs can be found here.

Tell me if you need more details on any of these.

  • Thanks for the reply. I'm looking for a way however to filter my posts by all three requirements at once. For example I would like to see all posts with the category 'HTML', posted in 'February 2017' by 'John'. – snookian Feb 14 '17 at 15:04
  • Alright. That is more complicated, i will try to write a code after i get back from work. – Jack Johansson Feb 14 '17 at 15:26
  • I have added some code that im working on to my posts, basically it calls rhe lists like you mentioned before and then in my template file call it in a form. it seems to be working but it creates a weird url is that normal? for example my blog page is using home.php and the url website.com/news lists the latest 20 posts, then when I use the filter thing it to select uncatorgized posts by admin the url is website.com/news/category/uncategorized/?user=1&submit=filter – snookian Feb 14 '17 at 15:42
  • Is type of url normal? – snookian Feb 14 '17 at 15:42
  • Yes it is. This is called passing data using GET method in php. If you don't want it to be in your URL, you can use the POST method. – Jack Johansson Feb 14 '17 at 15:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.