Pagination not working for ACF Query on front-page.php - wordpress

I'm using the following query based on the ACF documentation. For some reason, the pagination isn't working. Can anyone tell me what I'm missing? The pagination code is working fine on an archive.php page.
front-page.php
<?php
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$args = array(
'paged' => $paged,
'posts_per_page' => 6,
'post_type' => 'airdrop',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'airdrop_type',
'value' => array('Airdrop'),
'compare' => 'IN',
)
)
);
$the_query = new WP_Query( $args ); ?>
<?php if( $the_query->have_posts() ): ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php get_template_part('airdrops/full-width'); ?>
<?php endwhile; ?>
<div class="col-md-12">
<?php html5wp_pagination(); ?>
</div>
<?php endif; ?>
<?php wp_reset_query(); ?>
pagination code in functions.php
add_action('init', 'html5wp_pagination');
function html5wp_pagination()
{
global $wp_query;
$big = 999999999;
echo paginate_links(array(
'base' => str_replace($big, '%#%', get_pagenum_link($big)),
'format' => '?paged=%#%',
'current' => max(1, get_query_var('paged')),
'total' => $wp_query->max_num_pages
));
}

check this
Pagination_Parameters
And http://prntscr.com/kt47rs
You don't need use add_action('init','...') function for pagination.
and use calling function like : html5wp_pagination($the_query );

Please check, replace with this
function html5wp_pagination()
{
global $the_query;
$big = 999999999;
echo paginate_links(array(
'base' => str_replace($big, '%#%', get_pagenum_link($big)),
'format' => '?paged=%#%',
'current' => max(1, get_query_var('paged')),
'total' => $the_query->max_num_pages
));
}

Using a plugin may not be the best solution but I ended up using WP-PageNavi which allows wp_query. This worked for me and solved the problem.
Documentation can be found here: http://scribu.net/wordpress/wp-pagenavi/wpn-2-74.html

Related

Wordpress - two loops in CPT archive page, exclude posts from main query

I have two loops in my archive-inzerat.php, first is custom loop, second is default loop:
<div class="container">
<div class="row">
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$featured_args = array(
'post_type' => 'inzerat',
'post_status' => 'publish',
'posts_per_page' => 10,
'paged' => $paged,
'orderby' => 'publish_date',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'is_featured',
'value' => '1',
)
)
);
$featured_query = new WP_Query( $featured_args );
if ( $featured_query->have_posts() ) : ?>
<div class="col-md-12">
<div class="row">
<?php
while ( $featured_query->have_posts() ) : $featured_query->the_post();
echo '<div class="col-md-3">';
get_template_part( 'content', 'inzerat' );
echo '</div>';
endwhile;
wp_reset_postdata();
?>
</div>
</div>
<?php
endif;
?>
<?php
if ( have_posts() ) : ?>
<div class="col-md-12">
<div class="row">
<?php
while ( have_posts() ) : the_post();
echo '<div class="col-md-3">';
get_template_part( 'content', 'inzerat' );
echo '</div>';
endwhile;
?>
</div>
<div class="clearfix"></div>
<div class="utf-pagination-container margin-top-20">
<nav class="pagination">
<?php
global $wp_query;
$total_pages = $wp_query->max_num_pages;
if ( $total_pages > 1 ) {
$current_page = max( 1, get_query_var( 'paged' ) );
echo paginate_links( array(
'base' => get_pagenum_link( 1 ) . '%_%',
'format' => '/page/%#%',
'current' => $current_page,
'total' => $total_pages,
'prev_text' => '<i class="fa fa-angle-left"></i>',
'next_text' => '<i class="fa fa-angle-right"></i>',
'type' => 'list',
'end_size' => 3,
'mid_size' => 3,
) );
}
wp_reset_postdata();
?>
<script>
jQuery('ul.page-numbers').addClass('pagination-custom');
//jQuery('ul.page-numbers li').addClass('page-item');
//jQuery('ul.page-numbers li a, ul.page-numbers li span').addClass('page-link');
jQuery('ul.page-numbers li span.current').parent().addClass('active');
</script>
</nav>
</div>
</div>
<?php
endif;
?>
</div>
</div>
In first loop I want to display only custom posts by custom field is_featured which is type boolean. In second loop I want to exclude these posts and display others to prevent duplications. Unfortunately, this code I use removes everything from the main loop:
function my_posts_filter( $query ) {
if ( ! is_admin() && $query->is_main_query() && is_post_type_archive( 'inzerat' ) ) {
$query->set( 'meta_query', array(
array(
'key' => 'is_featured',
'value' => '1',
'compare' => 'NOT IN'
)
));
}
}
add_action( 'pre_get_posts', 'my_posts_filter' );
What's wrong here?
I want also ask, will my pagination work and not break when adding custom loop?
Thanks for helping.
As per my opinion try this logic,
Define a blank array at the beginning of your file.
$firstloop_ids = array();
When your first loop is performed, you just need to add those IDs into that blank array. ( you can use array_push for that, reference: https://www.w3schools.com/php/func_array_push.asp )
For the second loop, you can exclude those ids using that array by this parameter.
'post__not_in' => $firstloop_ids,
This should work for you!
Try with post__not_in argument:
$featured_args = array(
'post_type' => 'inzerat',
'post_status' => 'publish',
'posts_per_page' => 10,
'paged' => $paged,
'orderby' => 'publish_date',
'order' => 'DESC',
'post__not_in' => array(278),
'meta_query' => array(
array(
'key' => 'is_featured',
'value' => '1',
)
)
);
It will be definitely exclude specific posts.

Get all Posts If has same custom field values in Posts

Trying to get all the posts having the same zipcode as metavalue. Thanks in advance for the help.
<?php
$query = new WP_Query( array(
'post_type'=> array('service'),
'posts_per_page' => -1,
'meta_query' => array( array(
'key'=> 'zipcode',
'value'=> ','.$zip.',',
'compare'=> 'LIKE'
) )
));
?>
<?php if ( $query->have_posts() ) :while ( $query->have_posts() ) : $query->the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php endwhile; // end of the loop. ?>
<?php wp_reset_query(); ?>
<?php else: ?>
No results found.
<?php endif; ?>
zipcode are numbers for example 12345. If posts have value 12345 in the custom field. then it should display all posts which have the 12345 value. The above code is working fine but displays only one post.
Following code will be the proper for the meta query.
$query_args = array(
'post_type' => 'service',
'posts_per_page' => -1,
'meta_query' => array(
array(
'value' => $zip,
'compare' => 'LIKE',
'key' => 'zipcode',
),
)
);
$query = new WP_Query($query_args);
<?php if ( $query->have_posts() ) :while ( $query->have_posts() ) : $query->the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php endwhile; // end of the loop. ?>
<?php wp_reset_query(); ?>
<?php else: ?>
No results found.
<?php endif; ?>
Hope it helps.
This does the job for me:
$popularCourses = new WP_Query(
array(
'post_type' => 'courses',
'posts_per_page' => -1,
'post_status' => 'publish',
'meta_key' => 'course-is-promoted',
'meta_value' => 'Yes',
'orderby' => 'date',
'order' => 'DESC'
)
);
There are two ways.
After watching this code i will suggest you just visit this link for your better understanding.
(1)
$args = array(
'meta_query' => array(
array(
'key' => 'Your_key',//Enter your meta key here
'value' => 'professionnel',//Enter you meta value
'compare' => '=',//Comparison type (option filed) .
)
)
);
$query = new WP_Query($args);
(2)
$output_loop = get_posts( array(
'meta_key' => 'Your_key',//Meta key
'meta_value' => 'Your_value',//Meta value
) );
Now just print_r($output_loop) for the better understanding.

Pagination not working with query_posts

I trying to add pagination to my WordPress website, it's working fine with blog page, but with custom category when click any link of pagination URL change, but content still the same.
Custom category code
<?php query_posts('category_name=sport');
if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php get_template_part('loop'); ?>
<?php endwhile; // конец цикла
else: echo '<p>Blank.</p>'; endif; ?>
<?php pagination(); ?>
Pagintion in function.php
function pagination() {
global $wp_query;
$big = 999999999;
$links = paginate_links(array(
'base' => str_replace($big,'%#%',esc_url(get_pagenum_link($big))),
'format' => '?paged=%#%',
'current' => max(1, get_query_var('paged')),
'type' => 'array',
'prev_text' => 'Назад',
'next_text' => 'Вперед',
'total' => $wp_query->max_num_pages,
'show_all' => false,
'end_size' => 4,
'mid_size' => 4,
'add_args' => false,
'add_fragment' => '',
'before_page_number' => '',
'after_page_number' => ''
));
if( is_array( $links ) ) {
echo '<ul class="pagination">';
foreach ( $links as $link ) {
if ( strpos( $link, 'current' ) !== false ) echo "<li class='active'>$link</li>";
else echo "<li>$link</li>";
}
echo '</ul>';
}
}
Please help
instead of: query_posts('category_name=sport');
try this:
$page = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'category_name' => 'sport',
'posts_per_page' => 6,
'paged' => $page
);
query_posts($args);
For further customization check the List of Query Vars
Try this:
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'posts_per_page' => 3,
'paged' => $paged,
'category_name' => 'sport'
);
$the_query = new WP_Query( $args );
?>
Hope this helps :)
Please check below code:
$page = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'work',
'categories'=>'sports',
'posts_per_page' => 2,
'paged' => $page,
);
query_posts($args);
if (have_posts()) : while (have_posts()) : the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
<?php endwhile;
else: echo '<p>Blank.</p>'; endif; ?>
<?php pagination(); ?>
You have to mention post type also as per the example
Hope this work for you.

wordpress wp_Query pagination with custom post_type issue

I'm trying to make pagination for result of two post types which is one custom post type question and the normal post
first of all when I show all result without pagination using 'posts_per_page' => -1; the function works fine
but the problem happen when I try to make pagination, as you can see pagination function works normally except the some last pages of the pagination (I guess which contains question post_type even recent post type of questions appears normally )
here you are my full code
<?php
$my_query = new WP_Query(
array(
'post_type' => array('question', 'post'),
'posts_per_page' => get_option('to_count_portfolio'), // -1 to show all results
'author' => $post->post_author,
'paged' => get_query_var('paged') ? get_query_var('paged') : 1
)
);
?>
<?php
while ( $my_query->have_posts() ) : $my_query->the_post();
?>
<span class="title">
<?php echo get_post_type(); ?>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</span>
<br />
<?php endwhile; ?>
<?php
$big = 999999999; // need an unlikely integer
echo paginate_links(
array(
'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $my_query->max_num_pages
)
);
?>
How can I make pagination work normally and navigate to all pagination links ?
Replace your code this:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
if ($paged == "1") {
$args = array(
'post_type' => array('question', 'post'),
'posts_per_page' => get_option('to_count_portfolio'), // -1 to show all results
'author' => $post->post_author,
'offset' => 0
);
} else {
$offset = $paged * 5;
$offset = $offset - 5;
$args = array(
'post_type' => array('question', 'post'),
'posts_per_page' => get_option('to_count_portfolio'), // -1 to show all results
'author' => $post->post_author,
'offset' => $offset
);
}
$loop = new WP_Query($args);
?>
<?php
if ($loop->have_posts()) :while ($loop->have_posts()) : $loop->the_post();
?>
<span class="title">
<?php echo get_post_type(); ?>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</span>
<br />
<?php endwhile; ?>
<div class="pagination-grp">
<?php
$big = 999999999; // need an unlikely integer
//$i=1;
echo paginate_links(array(
'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))),
'format' => '?paged=%#%',
'current' => max(1, get_query_var('paged')),
'prev_text' => __('<'),
'next_text' => __('>'),
'total' => $loop->max_num_pages
));
wp_reset_postdata();
endif;
?>
</div>
install this free plugin https://wordpress.org/plugins/wp-pagenavi/
then refer below code
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array (
'post_type' => 'post',
'cat' => '53',
'paged' => $paged,
'posts_per_page' => '10',
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
get_template_part('parts/bl`enter code here`ogloop');
}
if(function_exists('wp_pagenavi'))
{
wp_pagenavi( array( 'query' => $query ) );
}
wp_reset_postdata();
} else {
echo 'no posts here';
}
This will show paging and work like a charm for you.
I guess that I figure it out
the solution is to edit the WP_Query args to be a custom number which is not same as site posts_per_page
'posts_per_page' => 11, // 13, or 20 ...
instead of
'posts_per_page' => get_option('to_count_portfolio'),
Why ? I've no idea, maybe the are some kind of conflict
someone can try it and see if that solution works with him/her same as me

Wordpress wp_query custom post type query not working on second page

Using below code i'm listing all posts under custom post type people
<?php $loop = new WP_Query(array('post_type' => 'people', 'order' => 'ASC', 'orderby' => 'meta_value', 'meta_key' => 'wpcf-people-sort-order','posts_per_page' => 4, 'paged' => get_query_var('paged') ? get_query_var('paged') : 1 )
); ?>
<div>Title: <?PHP the_title(); ?></div>
<div>Description: <?php echo esc_html( get_post_meta( $postid, 'wpcf-people-desscription', true ) ); ?> </div>
<?php endwhile; ?>
Below is my pagination,
<div class="cus-pagi">
<?php
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $loop->max_num_pages
) );
?>
</div>
Also i created a plugin using above code to display list of titles in sidebar. So whenever i access mysite.com/people both(i.e list of custom posts with pagination & sidebar list of post title) of my custom query are working fine.
If i go to second page, sidebar is showing empty.
did anyone know where i'm going wrong ?
You you need to use the Loop with your custom query:
<?php
$loop = new WP_Query(
array(
'post_type' => 'people',
'order' => 'ASC',
'orderby' => 'meta_value',
'meta_key' => 'wpcf-people-sort-order',
'posts_per_page' => 4,
'paged' => get_query_var('paged') ? get_query_var('paged') : 1 )
);
if ($loop->have_posts()) : while ($loop->have_posts()) : $loop->the_post();
?>
<div>Title: <?PHP the_title(); ?></div>
<div>Description: <?php echo esc_html( get_post_meta( $postid, 'wpcf-people-desscription', true ) ); ?> </div>
<?php endwhile; endif; wp_reset_postdata(); ?>

Resources