I'm new to loop customisation and I have an issue getting my search page front end filter loop working in the same way as my other category template post loop.
This is the loop I am using for the category templates which works fine.
<?php $custom_query_args = array( 'cat' => '2', 'posts_per_page' => 6 );
$custom_query_args['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$custom_query = new WP_Query( $custom_query_args );
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $custom_query;
if ( $custom_query->have_posts() ) :
while ( $custom_query->have_posts() ) :
$custom_query->the_post(); ?>
This allows me to paginate through all posts in this category but I need to convert a similar code block on my search page to allow it to do the same, currently the search template code below doesn't paginate like the above.
<?php wp_reset_postdata();
global $wp_query;
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
foreach($_POST['filter_cat'] as $fcat){$fcat_list .= $fcat . ",";}
query_posts(
array_merge(
array(
'cat' => $fcat_list,
'posts_per_page' => 6,
'paged' => $paged
),
$wp_query->query
)
); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
I know that the first code block is a better method but I can't figure out how to use the second block in the same way.
Edit:
Ok please excuse my inexperience.
Thanks to Pieter Goosen I'm part way there with using pre_get_posts.
I've done so successfully with category template but using the following for the search page template kills the site, I'm not sure where I'm going wrong.
function paginate_search ( $query ) {
if( $query->!is_admin() ) {
foreach($_POST['filter_cat'] as $fcat){$fcat_list .= $fcat . ",";}
query_posts( array_merge ( array (
'cat' => $fcat_list,
'posts_per_page' => 6,
'paged' => $paged
),
$wp_query->query
) ); } }
add_action( 'pre_get_posts', 'paginate_search' );
Related
I have an issue trying to get the current page from WP pagination. Here is my code:
global $wp_query;
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
var_dump(get_query_var( 'paged' )); // HERE RETURN 0 EVERYTIME
$args = [
'post_type' => 'post',
'posts_per_page' => 30,
'paged' => $paged
];
$loop = new WP_Query($args);
while ($loop->have_posts()) :
$loop->the_post(); ?>
// code ....
<?php endwhile; ?>
I use this code on a single-page template (and so from a single-page URL). Doing get_query_var( 'paged' ); returns 0.
How to get the current page from a single-page template?
As per documentation:
For getting the current pagination number on a static front page (Page
template) you have to use the 'page' query variable:
<?php $page = (int)get_query_var( 'page', 1 ); ?>
<h1>Currently Browsing Page <?php echo $page; ?> On a static front page</h1>
I have two custom post types for my site, but am using the same Categories/Tags for both.
I am trying to create a category.php page that will allow me to show all items within that category, but in two separate areas, one for each type.
I can get the first post type to display in the main loop, but how can I structure a second loop to display only the posts in the second type that are of that category?
I suggest you use to instances of WP_Query and in args section, for both sections have set post_type= for the desired one.
<?php
//general code to get the current category id
$category = get_category( get_query_var( 'cat' ) );
$cat_id = $category->cat_ID;
$args = array(
'post_type' => 'post',
'posts_per_page' => 5,
'cat' => $cat_id,
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
echo get_the_title();
echo get_the_excerpt();
endwhile;
wp_reset_postdata();
//for second loop of second post type
$args1 = array(
'post_type' => 'your_post_type',
'posts_per_page' => 5,
'cat' => $cat_id,
);
$the_query1 = new WP_Query( $args1 );
if ( $the_query1->have_posts() ) :
while ( $the_query1->have_posts() ) : $the_query1->the_post();
echo get_the_title();
echo get_the_excerpt();
endwhile;
wp_reset_postdata();
I am trying to get working pagination in my WP_Query.
After trying more then 2 hour, no way except stackoverflow :-D.
What is my problem
Older and newer pagination links are appreaing and when I click on them, then it take me to the correct url which is : /?paged=2.
But post list did not change, same post on every page.
Here is my code
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$args = array(
'cat' => $cat,
(($paged != '') ? 'paged =>'. $paged : ''),
'posts_per_page' => $post_to_show
);
print_r($args);
$the_query = new WP_Query($args);
while ($the_query->have_posts()) : $the_query->the_post();
//post template
endwhile;
if ( get_next_posts_link() || get_previous_posts_link() ) {
echo '<div class="wp-navigation clearfix">
<div class="alignleft">'.next_posts_link('« Older Entries').'</div>
<div class="alignright">'.previous_posts_link('Newer Entries »').'</div>
</div>';
}
wp_reset_query();
Your $args array looks wrong. Also, $paged will never be empty (because it always gets assigned the default value of 1), so your check is redundant.
There is nothing wrong with passing 1 as page number.
$args = array(
'cat' => $cat,
'paged' => $paged,
'posts_per_page' => $post_to_show
);
I'm printing posts and I want to get number of results, how can I do that?
This is part of my code:
if (have_posts()) :
$args = array(
'showposts' => '5',
'paged' => $paged
);
$thePosts = query_posts($args);
...
Thank's for help
SOLVED:
if (have_posts()) :
$args = array(
'showposts' => '5',
'paged' => $paged
);
$thePosts = query_posts($args);
global $wp_query;
echo $wp_query->found_posts;
...
To display the number of results of a search, use:
Search Result for
<?php
/* Search Count */
$allsearch = &new WP_Query("s=$s&showposts=-1");
$key = wp_specialchars($s, 1);
$count = $allsearch->post_count; _e('');
_e('<span class="search-terms">');
echo $key; _e('</span>');
_e(' — ');
echo $count . ' ';
_e('articles');
wp_reset_query();
?>
This was taken from: WP Beginner.
The correct answer is
if (have_posts()) :
$args = array(
'showposts' => '5',
'paged' => $paged
);
$thePosts = query_posts($args);
echo $thePosts ->found_posts;
...
This will give you the results: Showing results 11-20 of 46, for instance.
$args = array(
'cat'=> $cat,
'posts_per_page' => 10,
'paged' => $paged,
's'=> $s
);
query_posts($args);
$startpost=1;
$startpost=10*($paged - 1)+1;
$endpost = (10*$paged < $wp_query->found_posts ? 10*$paged : $wp_query->found_posts);
?>
<h2 class="displayResult">Showing results <?php echo $startpost; ?> - <?php echo $endpost; ?> of <?php echo $wp_query->found_posts; ?></h2>
If this is not a search page, simply remove the line "'s'=> $s".
If you do need it, make sure you declare the variable as $_GET['s'] above.
Easy. To display number of results for this current page, use
// Showing Page X of Y
print filter_var( absint( $GLOBALS['wp_query']->post_count ), FILTER_SANITIZE_NUMBER_INT );
For the total amount of results, use
print filter_var( absint( $GLOBALS['wp_query']->found_posts ), FILTER_SANITIZE_NUMBER_INT );
display numbers of search results :
<?php global $wp_query;
echo $wp_query->post_count; ?>
query_posts( $args );
global $wp_query;
print_r($wp_query->max_num_pages);
It help me.
I’m using the WP PageNavi plugin for pagination. This particular problem in not getting the taxonomy-portflio-category.php page to paginate is also a problem when WP PageNavi is turned off.
I’ve had a heck of a time getting pagination to work on the homepage and on a page template page, but I did get them to work. Here’s their code:
page-home.php (used as a Page template on a static front page called “Home”)
$paged = 1;
if ( get_query_var('paged') ) $paged = get_query_var('paged');
if ( get_query_var('page') ) $paged = get_query_var('page');
$i = 0;
$loop = new WP_Query( array( 'post_type' => 'portfolio', 'paged' => $paged, 'posts_per_page' => 24 ) );
while ( $loop->have_posts() ) : $loop->the_post();
// output
$i++; endwhile;
if ( function_exists( 'wp_pagenavi' ) ) {
wp_pagenavi( array( 'query' => $loop ) );
wp_reset_postdata();
}
Pagination works!
page-portfolio.php (used as a Page template on a Page called “Work”)
$i = 0;
$loop = new WP_Query( array( 'post_type' => 'portfolio', 'paged' => get_query_var( 'paged' ), 'posts_per_page' => 24 ) );
while ( $loop->have_posts() ) : $loop->the_post();
// output
$i++; endwhile;
if ( function_exists( 'wp_pagenavi' ) ) {
wp_pagenavi( array( 'query' => $loop ) );
wp_reset_postdata();
}
Pagination works!
taxonomy-portfolio-category.php (used as a way to display portfolio sections e.g. print, photography, etc.)
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
global $wp_query;
query_posts( array_merge( $wp_query->query, array( 'posts_per_page' => 2 ) ) );
if (have_posts()) : while ( have_posts() ) : the_post();
// output
endwhile; endif;
if ( function_exists( 'wp_pagenavi' ) ) {
wp_pagenavi();
}
Page 1 (/portfolio/interactive/) looks great! It’s definitely only posting 2 items and it calculates the correct number of pagination pages. But when you click on page 2 or 3 or 4 (/portfolio/interactive/page/2/) the site defaults to index.php and shows “Page not found”. Pagination fails!
Hopefully I can resolve this soon. I’ve seen A LOT of people with this same problem of pagination on custom taxonomy pages, but no solid solutions. Please help!
You Need to set posts per page to 24 on the Settings -> Reading page in WP admin. Hope this helps someone.
I have tried using WP-Pagenavi but it never worked so i used the pagination from Wordpress it self, i used the twentyfourteen_paging_nav() function form Twentyfourteen becuse it has a taxonomy page, here is the code:
if ( ! function_exists( 'twentyfourteen_paging_nav' ) ) :
function twentyfourteen_paging_nav() {
global $wp_query, $wp_rewrite;
// Don't print empty markup if there's only one page.
if ( $wp_query->max_num_pages < 2 ) {
return;
}
$paged = get_query_var( 'paged' ) ? intval( get_query_var( 'paged' ) ) : 1;
$pagenum_link = html_entity_decode( get_pagenum_link() );
$query_args = array();
$url_parts = explode( '?', $pagenum_link );
if ( isset( $url_parts[1] ) ) {
wp_parse_str( $url_parts[1], $query_args );
}
$pagenum_link = remove_query_arg( array_keys( $query_args ), $pagenum_link );
$pagenum_link = trailingslashit( $pagenum_link ) . '%_%';
$format = $wp_rewrite->using_index_permalinks() && ! strpos( $pagenum_link, 'index.php' ) ? 'index.php/' : '';
$format .= $wp_rewrite->using_permalinks() ? user_trailingslashit( $wp_rewrite->pagination_base . '/%#%', 'paged' ) : '?paged=%#%';
// Set up paginated links.
$links = paginate_links( array(
'base' => $pagenum_link,
'format' => $format,
'total' => $wp_query->max_num_pages,
'current' => $paged,
'mid_size' => 1,
'add_args' => array_map( 'urlencode', $query_args ),
'prev_text' => __( '← Previous', 'twentyfourteen' ),
'next_text' => __( 'Next →', 'twentyfourteen' ),
) );
if ( $links ) :
?>
<nav class="pagination-contaner" role="navigation">
<h1 class="screen-reader-text"><?php _e( 'Posts navigation', 'twentyfourteen' ); ?></h1>
<ul class="pagination">
<?php echo $links; ?>
</ul><!-- .pagination -->
</nav><!-- .navigation -->
<?php
endif;
}
endif;
I ran into similar issue, it took me hours of googling! I found the solution at last.
Add the following code to functions.php in your theme folder:
$option_posts_per_page = get_option( 'posts_per_page' );
add_action( 'init', 'my_modify_posts_per_page', 0);
function my_modify_posts_per_page() {
add_filter( 'option_posts_per_page', 'my_option_posts_per_page' );
}
function my_option_posts_per_page( $value ) {
global $option_posts_per_page;
if ( is_tax( 'portfolio-category') ) {
return 2;
} else {
return $option_posts_per_page;
}
}
URL for the solution
May be you need to enable search to enable pagination
While declaring custom taxonomy you should disable search excluding.
exclude_from_search => false
This fixed my problem.
I would like to share following solution (add this code to functions.php in your theme):
function fix_taxonomy_pagination ( $query ) {
// not an admin page and it is the main query
if (!is_admin() && $query->is_main_query()){
if(is_tax()){
// where 24 is number of posts per page on custom taxonomy pages
$query->set('posts_per_page', 24);
}
}
}
add_action( 'pre_get_posts', 'fix_taxonomy_pagination' );
source