Advance Search not showing proper result in Wordpress? - wordpress

I have added advance search in Wordpress Search.. please check below image..
Below is my search Code in wordpress :
global $wp_query;
$args = array (
's' => $s,
'cat' => $category,
'year' => $year,
'monthnum' => $monthnum
);
$query = new WP_Query($args) ;
<?php if ( $query->have_posts() ) : ?>
<?php while (have_posts() ) : the_post(); ?>
<article>
<h2><?php the_title(); ?></h2>
<div class="entry">
<?php the_content( __( 'Read More »', 'tie' ) ); ?>
</div>
</article>
<?php endwhile; ?>
<?php endif; ?>
From above code i am getting proper result... but when i change the category the result stays same...??
If i get search results as 1,2,3,4,5,6,7,8,9 and on change on category i need 1,5,6,8,9
what is wrong with my code..??

First check your $category is null or not. if it not showing the related to category.
also make sure you $category is integer
See what WP_QUERY use category arguments.
cat (int) - use category id. category_name (string) - use
category slug. category__and (array) - use category id.
category__in (array) - use category id. category__not_in (array)
- use category id.
Change your loop to below code.
For using pagination you may need to change your $args array with the required arguments.
// for number of page.
posts_per_page , paged used for pagination.
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$per_page = 8; // records per page.
$args = array (
's' => $s,
'cat' => $category,
'year' => $year,
'monthnum' => $monthnum ,
'posts_per_page' => $per_page,
'post_status' => 'publish',
'paged' => $paged,
);
// the query
$query = new WP_Query( $args ); ?>
<?php if ( $query->have_posts() ) : ?>
<!-- pagination here -->
<!-- the loop -->
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<!-- end of the loop -->
<!-- pagination here -->
<?php if (function_exists("pagination_custom")) {
pagination_custom($query->max_num_pages);
}
?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
UPDATE : For Pagination:
// Pagination code
function pagination_custom($pages = '', $range = 4)
{
$showitems = ($range * 2) + 1;
global $paged;
if (empty($paged)) $paged = 1;
if ($pages == '') {
global $wp_query;
$pages = $wp_query->max_num_pages;
if (!$pages) {
$pages = 1;
}
}
if (1 != $pages) {
echo "<div class=\"pagination\"><ul>";
if ($paged > 2 && $paged > $range + 1 && $showitems < $pages) echo "<a href='" . get_pagenum_link(1) . "'>« First</a>";
if ($paged > 1 && $showitems < $pages) echo "<a href='" . get_pagenum_link($paged - 1) . "'>‹ Previous</a>";
for ($i = 1; $i <= $pages; $i++) {
if (1 != $pages && (!($i >= $paged + $range + 1 || $i <= $paged - $range - 1) || $pages <= $showitems)) {
echo ($paged == $i) ? "<li>" . $i . "</li>" : "<li>" . $i . "</li>";
}
}
if ($paged < $pages && $showitems < $pages) echo "Next ›";
if ($paged < $pages - 1 && $paged + $range - 1 < $pages && $showitems < $pages) echo "<a href='" . get_pagenum_link($pages) . "'>Last »</a>";
echo "</ul></div>\n";
}
}

Related

Custom Post Type Pagination sends me to home page

For years I use a custom pagination script that I include to all my websites and it paginates flawlessly through posts. For the first time ever I created custom post types. My same script will not paginate through pages... Also all pages links send me to home. It displays exactly the correct pages number based on posts per page. It just won't work. I have used all scripts I could find on net.
Is there anything I am missing?
Has archive is true by the way. Below code example is from archive-drivers.php
I also used both custom CPT creation with script in functions and also used a plugin. Just to check if something was wrong.
<?php
$args = array(
'posts_per_page' => 1,
'paged' => $paged,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'Drivers',
'post_status' => 'publish',
'suppress_filters' => true
);
// get the results
$wp_query = new WP_Query( $args );
?>
<?php if ($wp_query->have_posts()): // the Loop ?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
(...my code to display the custom post type posts...)
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<!-- end of Code for the Loop -->
<?php endif; ?>
<!-- Code for PAGINATION -->
<?php
if (function_exists("custom_pagination"))
{
custom_pagination();
}
?>
<!-- End of Code for PAGINATION -->
<?php wp_reset_postdata(); ?>
<!-- end of Code for the Loop -->
and
// Bootstrap Custom Pagination function in Functions.php
function custom_pagination($pages = '', $range = 4)
{
$showitems = ($range * 2) + 1;
global $paged;
if(empty($paged)) $paged = 1;
if($pages == '')
{
global $wp_query;
$pages = $wp_query->max_num_pages;
if(!$pages)
{
$pages = 1;
}
}
if(1 != $pages)
{
echo '<div class="as-pagination" style="text-align:center; align:center;">';
echo '<div> Page ' .$paged . ' of ' .$pages.'</div>';
echo '<ul>';
// If we want to always see th First Page link
if($paged > 1) echo '<li></i></li>';
for ($i=1; $i <= $pages; $i++)
{
if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
{
echo ($paged == $i)? "<li class=\"active\"><span>".$i."</span>
</li>":"<li><a href='".get_pagenum_link($i)."'>".$i."</a></li>";
}
}
//if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo '<li>»</li>';
if($paged < $pages) echo '<li><i class="far fa-arrow-right"></i></li>';
echo "</ul>";
echo "</div>";
}
}
And the answer came from a FB friend and has to do with pre_get_posts. Only that way you can tell wp how many posts per page you want for your custom post type. The 'posts_per_page' in args won't work.
function my_cptui_change_posts_per_page( $query ) {
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
if ( is_post_type_archive( 'drivers' ) ) {
$query->set( 'posts_per_page', 1 );
}
}
add_filter( ' ', 'my_cptui_change_posts_per_page' );

Pagination on an Advanced Custom Fields Repeater Stopped Working

Not sure if this was since upgrading to PHP 7.2 or the latest WordPress version but the following code used to allow me to add pagination for repeater values. Now what seems to happen is that the page just reloads the page. The pagination link shows as /example/2/, this used to load the page but now it is just reloading example.
Any ideas?
<?php
/*
* Paginatation on Advanced Custom Fields Repeater
*/
if ( get_query_var('paged') ) {
$page = get_query_var('paged');
} elseif ( get_query_var('page') ) {
$page = get_query_var('page');
} else {
$page = 1;
}
// Variables
$row = 0;
$images_per_page = 10; // How many images to display on each page
$images = get_field( 'image_gallery' );
$total = count( $images );
$pages = ceil( $total / $images_per_page );
$min = ( ( $page * $images_per_page ) - $images_per_page ) + 1;
$max = ( $min + $images_per_page ) - 1;
// ACF Loop
if( have_rows( 'image_gallery' ) ) : ?>
<?php while( have_rows( 'image_gallery' ) ): the_row();
$row++;
// Ignore this image if $row is lower than $min
if($row < $min) { continue; }
// Stop loop completely if $row is higher than $max
if($row > $max) { break; } ?>
<?php $img_obj = get_sub_field( 'image' ); ?>
<a href="<?php echo $img_obj['sizes']['large']; ?>">
<img src ="<?php echo $img_obj['sizes']['thumbnail']; ?>" alt= "Your ALT Tag" />
</a>
<?php endwhile;
// Pagination
echo paginate_links( array(
'base' => get_permalink() . '%#%' . '/',
'format' => '?paged=%#%',
'current' => $page,
'total' => $pages
) );
?>
<?php else: ?>
<p>No images found</p>
<?php endif; ?>
There was a bug tracker after 5.5:
https://core.trac.wordpress.org/ticket/50976#comment:7
You might check some of the solutions there. It seems to be an issue with the query var "page" which is reserved in WordPress core, since it normally uses ?p and ?page to redirect to a certain page.
So you might use something else.

How to add Custom pagination in wordpress

i have created custom theme in wordpress.
I want to add custom pagination to my custom post template which is INDEX.PHP
Can you please check what is wrong in pagination script. actually i have set 4 post limit and there around 8 posts in my blog.. when click on 2 pagination, it won't move to next page...
<?php get_header(); ?>
<?php
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
// WP_Query arguments
$args = array (
'post_type' => the_post(),
'posts_per_page' => '3',
'paged' => $paged
);
?>
<?php
// The Query
$cquery = new WP_Query( $args );
while ( $cquery->have_posts() ) : $cquery->the_post();
?>
<div class="row">
<div class="img"><?php the_post_thumbnail('full'); ?></div>
<div class="text">
<h2><?php the_title(); ?></h2>
<h3 style="line-height: 1px;"><span class="floatL">By </span> <span class="floatL"> <?php the_author_posts_link(); ?> </span> <span class="floatL"> - </span> <span class="floatL"><?php the_time('F jS, Y'); ?></span><div class="clr"></div></h3>
<p><?php the_content('Read More') ?></p>
</div>
</div>
<?php
$post->ID;
endwhile;
$big = 999999999; // need an unlikely integer
?>
<div class="row">
<div class="pagination">
<?php
echo paginate_links( array(
'base' => str_replace( $big, '', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $cquery->max_num_pages
) );
?>
</div>
</div>
<?php get_footer(); ?>
Please help :)
Thank you,
Harshad Patil
Try this
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
// WP_Query arguments
$args = array (
'post_type' => 'YOUR_CUSTOM_POST_TYPE',
'posts_per_page' => '3',
'paged' => $paged
);
// The Query
$cquery = new WP_Query( $args );
while ( $cquery->have_posts() ) : $cquery->the_post();
echo $post->ID;
endwhile;
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $cquery->max_num_pages
) );
Add Following code in "functions.php" file:
function custom_pagination($pages = '', $range = 2)
{
$showitems = ($range * 2)+1;
global $paged;
if(empty($paged)) $paged = 1;
if($pages == '')
{
global $wp_query;
$pages = $wp_query->max_num_pages;
if(!$pages)
{
$pages = 1;
}
}
if(1 != $pages)
{
echo "<div class='pagination'>";
if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>«</a>";
if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>‹</a>";
for ($i=1; $i <= $pages; $i++)
{
if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
{
echo ($paged == $i)? "<span class='current'>".$i."</span>":"<a href='".get_pagenum_link($i)."' class='inactive' >".$i."</a>";
}
}
if ($paged < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($paged + 1)."'>›</a>";
if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>»</a>";
echo "</div>\n";
}
}
And in your "index.php" file add "custom_pagination();" after while loop.

Wordpress: How to add pagination to page

I have 6 posts in Wordpress. I'm trying to display last 5 posts on page. Here is my code:
<?php
/*
Template Name: Posts Template
*/
?>
//header...
<?php $the_query = new WP_Query( array('showposts' => 5, 'post_type' => 'post')); ?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<h1><?php echo the_title(); ?></h1>
<?php endwhile; ?>
<?php posts_nav_link(); ?>
//footer
Pagination shows me that there is 4 pages, but like I said I have only 6 posts, so how it is possible? Also Pagination seems to not work correctly, doesn't matter on what page I am, it shows always last 5 posts.
Any idea what am I doing wrong?
Try adding a paged parameter to your query. You can also have a read of the documentation on the topic, there are some examples. Oh, and it's probably more forward-looking to use posts_per_page instead of showposts.
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$the_query = new WP_Query( array(
'posts_per_page' => 5,
'post_type' => 'post',
'paged' => $paged
));
Add this code to functions.php
function posts_nav_link( $query_object, $show_previous_posts_link = false, $show_next_posts_link = false ) {
if( is_singular() )
return;
/** Stop execution if there's only 1 page */
if( $query_object->max_num_pages <= 1 )
return;
$paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
$max = intval( $query_object->max_num_pages );
/** Add current page to the array */
if ( $paged >= 1 )
$links[] = $paged;
/** Add the pages around the current page to the array */
if ( $paged >= 3 ) {
$links[] = $paged - 1;
$links[] = $paged - 2;
}
if ( ( $paged + 2 ) <= $max ) {
$links[] = $paged + 2;
$links[] = $paged + 1;
}
echo '<div class="navigation"><ul>' . "\n";
/** Previous Post Link */
if ( get_previous_posts_link() && $show_previous_posts_link)
printf( '<li>%s</li>' . "\n", get_previous_posts_link() );
/** Link to first page, plus ellipses if necessary */
if ( ! in_array( 1, $links ) ) {
$class = 1 == $paged ? ' class="active"' : '';
printf( '<li%s>%s</li>' . "\n", $class, esc_url( get_pagenum_link( 1 ) ), '1' );
if ( ! in_array( 2, $links ) )
echo '<li>…</li>';
}
/** Link to current page, plus 2 pages in either direction if necessary */
sort( $links );
foreach ( (array) $links as $link ) {
$class = $paged == $link ? ' class="active"' : '';
printf( '<li%s>%s</li>' . "\n", $class, esc_url( get_pagenum_link( $link ) ), $link );
}
/** Link to last page, plus ellipses if necessary */
if ( ! in_array( $max, $links ) ) {
if ( ! in_array( $max - 1, $links ) )
echo '<li>…</li>' . "\n";
$class = $paged == $max ? ' class="active"' : '';
printf( '<li%s>%s</li>' . "\n", $class, esc_url( get_pagenum_link( $max ) ), $max );
}
/** Next Post Link */
if ( get_next_posts_link() && $show_next_posts_link )
printf( '<li>%s</li>' . "\n", get_next_posts_link() );
echo '</ul></div>' . "\n";
}
And use posts_nav_link($the_query):
<?php
/*
Template Name: Posts Template
*/
?>
//header...
<?php $the_query = new WP_Query( array('showposts' => 5, 'post_type' => 'post')); ?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<h1><?php echo the_title(); ?></h1>
<?php endwhile; ?>
<?php posts_nav_link($the_query); ?>
//footer

Trying to implement pagination for a specific category ( WORDPRESS )

I implemented a custom post type for a discography.
I managed to edit my template's loop, so it shows all the posts with the "Track" type, but now the pagination is not working anymore.
This is the code in the loop :
<?php
$args = array( 'post_type' => 'Track', 'posts_per_page' => 5);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
//HERE COMES MY HTML STUFF
<?php endwhile;?>
<?php fuse_pagenavi(); ?>
And this is the code for my fuse_pagenavi() :
<?php
function fuse_pagenavi($pages = '', $range = 4)
{
$showitems = ($range * 2)+1;
global $paged;
if(empty($paged)) $paged = 1;
if($pages == '')
{
global $wp_query;
$pages = $wp_query->max_num_pages;
if(!$pages)
{
$pages = 1;
}
}
if(1 != $pages)
{
echo "<div class=\"pagination\"><span class=\"pageof\">Page ".$paged." of ".$pages."</span>";
if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>« First</a>";
if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>‹ Previous</a>";
for ($i=1; $i <= $pages; $i++)
{
if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
{
echo ($paged == $i)? "<span class=\"current\">".$i."</span>":"".$i."";
}
}
if ($paged < $pages && $showitems < $pages) echo "<a class=\"pageof\" href=\"".get_pagenum_link($paged + 1)."\">Next ›</a>";
if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "<a class=\"pageof\" href='".get_pagenum_link($pages)."'>Last »</a>";
echo "</div>\n";
}
}
?>
Thank you in advance !
UPDATE :
Ok I think I make some progress.
I installed the wp-pagenavi plugin and now i see the pagination links at the bottom of the page but they point to mysite.com/?paged=x instead of pointing to mysite.com/?page=x.... i tested manually with 'page' and it works...
The code is the following :
<?php
$paged = get_query_var('page');
$my_query = new WP_Query($args);
$args = array( 'post_type' => 'Track', 'posts_per_page' => 5,'paged' => $paged);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
//HTML STUFF
<?php
wp_pagenavi(array('query' => $loop));
wp_reset_postdata();
?>
How can I make my pagination links to point to mysite.com?page=x instead of paged=x.
Thanks
try this:
<?php
if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
else { $paged = 1; }
$args = array( 'post_type' => 'Track', 'posts_per_page' => 5, 'paged' => $paged);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
//HERE COMES MY HTML STUFF
<?php endwhile;?>
<?php fuse_pagenavi(); ?>
Are you using loop inside of loop. If so, I think this : https://wordpress.stackexchange.com/questions/89191/using-query-posts-inside-single-php-loop is useful for you.
Good luck

Resources