Created
March 7, 2023 13:39
-
-
Save jessekanner/4150e38345551ebad3e4834df88939b6 to your computer and use it in GitHub Desktop.
WordPress - Custom WP_Query with pagination
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| global $wp_query; | |
| $paged = get_query_var('paged', 1); | |
| $args = array( | |
| 'post_type' => '{your_post_type_name}', | |
| 'meta_query' => array('{add your meta query argument if need}'), | |
| 'orderby' => 'modified', | |
| 'order' => 'DESC', | |
| 'posts_per_page' => 20, | |
| 'paged' => $paged | |
| ); | |
| $query = new WP_Query($args); | |
| if($query->have_posts()): | |
| while ($query->have_posts()) : $query->the_post(); | |
| //add your code here | |
| endwhile; | |
| wp_reset_query(); | |
| //manage pagination based on custom Query. | |
| $GLOBALS['wp_query']->max_num_pages = $query->max_num_pages; | |
| the_posts_pagination(array( | |
| 'mid_size' => 1, | |
| 'prev_text' => __('Previous page', 'patelextensions'), | |
| 'next_text' => __('Next page', 'patelextensions'), | |
| 'before_page_number' => '<span class="meta-nav screen-reader-text">' . __('Page', 'patelextensions') . ' </span>', | |
| )); | |
| else: | |
| ?> | |
| <div class="container text-center"><?php echo _d('Result not found','30'); ?></div> | |
| <?php | |
| endif; | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment