archives - Post filter Month dropdown at front-end like wordpress backend

admin2025-01-07  4

when i run the following code.

$args = array(
    'type'            => 'monthly',
    'limit'           => '',
    'format'          => 'option', 
    'before'          => '',
    'after'           => '',
    'show_post_count' => false,
    'echo'            => 1,
    'order'           => 'DESC',
    'post_type'     => 'post'
)
<select>
<option value=""> Select </option>
<?php 
    wp_get_archives( $args ); 
?>  </select>

I got this output

<select>
<option> Select </option>
    <option value="/"> March 2016 </option>
<option value="/"> February 2016 </option></select>

but i need output like WordPress back end filter option. like this

    <select name="m" id="filter-by-date"><option selected="selected" value="0">All dates</option><option value="201603">March 2016</option><option value="201602">February 2016</option></select>

Thanks,

when i run the following code.

$args = array(
    'type'            => 'monthly',
    'limit'           => '',
    'format'          => 'option', 
    'before'          => '',
    'after'           => '',
    'show_post_count' => false,
    'echo'            => 1,
    'order'           => 'DESC',
    'post_type'     => 'post'
)
<select>
<option value=""> Select </option>
<?php 
    wp_get_archives( $args ); 
?>  </select>

I got this output

<select>
<option> Select </option>
    <option value="http://test.astech-us.com/holistichivorce/2016/03/"> March 2016 </option>
<option value="http://test.astech-us.com/holistichivorce/2016/02/"> February 2016 </option></select>

but i need output like WordPress back end filter option. like this

    <select name="m" id="filter-by-date"><option selected="selected" value="0">All dates</option><option value="201603">March 2016</option><option value="201602">February 2016</option></select>

Thanks,

Share Improve this question asked Mar 18, 2016 at 9:26 shubham jainshubham jain 161 silver badge6 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

wp_get_archives creates its links with the function get_archives_link. this function returns plain HTML, but it has a filter you can hook into.

You can use the get_archives_link filter to manipulate your HTML with some regex.

function my_archives_link($link_html) {
    //TODO: my regex to manipulate the HTML
    return $link_html;
}

add_filter('get_archives_link','my_archives_link')

Further reading

  • wp_get_archives
  • get_archives_link
  • Filter Hook: get_archives_link

To create a dropdown filter for posts by month in the front end similar to wp backend, you can use the wp_dropdown_categories() function with a custom taxonomy query to achieve the desired output

<select name="filter-by-date" id="filter-by-date">
    <option selected="selected" value="0">All dates</option>
    <?php
    global $wpdb;
    $months = $wpdb->get_results("
        SELECT DISTINCT YEAR(post_date) AS year, MONTH(post_date) AS month
        FROM $wpdb->posts
        WHERE post_type = 'post' AND post_status = 'publish'
        ORDER BY post_date DESC
    ");
    foreach ($months as $month) {
        $value = sprintf("%04d%02d", $month->year, $month->month);
        $label = date("F Y", strtotime($month->year . '-' . $month->month . '-01'));
        echo "<option value='$value'>$label</option>";
    }
    ?>
</select>
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736252422a44.html

最新回复(0)