简体   繁体   中英

How can i make “date pagination” with wordpress

i have this code in my wordpress site.

$today = date("Y-m-d"); //Today's posts
        $args = array( 
                    'post_type' => 'questions', 
                    'meta_key' => '_question_date', 
                    'posts_per_page' => 20, 
                    'order' => 'DESC',
                    'meta_query' => array(
                        array(
                           'key' => '_question_date',
                           'value' => $today,
                           'compare' => '=',
                           'type' => 'CHAR'
                       )
                    )
 );

can i pagination this with custom meta query date? please help me!

You can use the Date Pagination plugin You can use the plugin functions in your theme after installation

Examples

These examples show you how to add Date Pagination to pages by seting the date_pagination_type query var.

Date pagination with pre_get_posts

You can set the type of date pagination to 'yearly', 'monthly' or 'daily' with the 'date_pagination_type' query variable.

add_action( 'pre_get_posts', 'monthly_paginated_home_query' );
function monthly_paginated_home_query( $query ) {

    // not a wp-admin page and the query is for the main query
    if ( !is_admin() && $query->is_main_query() ) {

        //  on the home page only
        if ( is_home() ) {

            // set the date pagination to 'monthly'
            $query->set('date_pagination_type', 'monthly'); 

            // set other arguments here
        }
    }
}

Date pagination with WP_Query

You can set the type of date pagination to 'yearly', 'monthly' or 'daily' with the 'date_pagination_type' argument.

In this example we set it to 'monthly'.

<?php
// Get the paged variable and use it in the custom query.
// (see: http://codex.wordpress.org/Pagination ).
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;

// Example arguments.
$args = array(
    // set the date pagination to monthly
    'date_pagination_type' => 'monthly', // 'yearly', 'monthly', 'daily'
    'paged'                => $paged,
);

// The custom query.
$the_query = new WP_Query( $args );
?>
<!-- The Loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<!-- Your theme's loop code here -->
<?php endwhile; ?>

This is my solution. it works very well. i hope this help to others.

            $today = date("Y-m-d");
            $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
                if($paged == 1) {
                    $getdate = $today;
                } 
                else if ($paged >= 2) {
                    $date = strtotime("-".$paged+1 ." days");
                    $getdate = date("Y-m-d",$date);
                }
            $args = array( 
                        'post_type' => 'questions', 
                        'meta_key' => '_question_date', 
                        'posts_per_page' => 20, 
                        'order' => 'DESC',
                        'meta_query' => array(
                            array(
                               'key' => '_question_date',
                               'value' => $getdate,
                               'compare' => '=', 
                               'type' => 'CHAR'
                           )
                        )
            );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM