why page 2 not working in wp query - php

Why http://localhost/wp/?paged=2 and older not working in wp_query?
I have movie and series from "custom post type" more than 15 posts.
It's separated in 2 pages in the front-end, but when I press the older post, the page is still first page.
Here is my index.php loop code:
<?php
$paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
$query_args = array(
'post_type' => array('post','series','movie'),
'posts_per_page' => 10,
'paged' => $paged
);
$the_query = new WP_Query( $query_args );
?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h1><?php the_title();?></h1>
<?php endwhile; ?>
<div class="col-md-12">
<?php if ($the_query->max_num_pages > 1) { // check if the max number of pages is greater than 1 ?>
<nav class="prev-next-posts">
<div class="prev-posts-link">
<?php echo get_next_posts_link( 'Older Entries', $the_query->max_num_pages ); // display older posts link ?>
</div>
<div class="next-posts-link">
<?php echo get_previous_posts_link( 'Newer Entries' ); // display newer posts link ?>
</div>
</nav>
<?php } ?>
</div>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<article>
<h1>Sorry...</h1>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
</article>
<?php endif; ?>

I've found a solution:
add_filter( 'pre_get_posts', 'my_get_posts' );
function my_get_posts( $query ) {
if ( $query->is_main_query() && is_home() ) {
$query->set( 'post_type', array( 'post', 'movie', 'series' ) );
}
if ( $query->is_main_query() && is_category() ) {
$query->set( 'post_type', array( 'post', 'movie', 'series' ) );
}
if ( $query->is_main_query() && is_search() ) {
$query->set( 'post_type', array( 'post', 'movie', 'series' ) );
}
if ( $query->is_main_query() && is_tag() ) {
$query->set( 'post_type', array( 'post', 'movie', 'series' ) );
}
return $query;
}
this code isn't stop appearing navigation menu

Related

Custom post type archive : display subcategories of current category

In my custom post type archive page, I would like to be able to filter posts by categories.
I have achieved this, but I can only manage to display all categories. I would like same level categories below the current category to be visible only.
For example if I have taxonomies like :
Category 1
-- Subcategory 1
--- subsub 1.1
-- Subcategory 2
When I am on the page https://example.com/mycpt/?getby=cat&cat=category-1
Only Subcategories 1 and Subcategory 2 should be visible
Here is my code:
In archive-mycpt.php
<div class="filter-custom-taxonomy">
<?php
$terms = get_terms( ['taxonomy' => 'product_cat'] );
foreach ( $terms as $term ) : ?>
<a href="<?php echo home_url() ?>/tutos/?getby=cat&cat=<?php echo esc_attr( $term->slug ); ?>">
<?php echo esc_html( $term->name ); ?>
</a>
<?php endforeach; ?>
</div>
<div class="row">
<div class="small-12 columns">
<?php do_action( 'thb_archive_title' ); ?>
<div class="row">
<?php
if ( have_posts() ) :
while ( have_posts() ) :
the_post();
get_template_part( 'inc/templates/post-styles/post-style1' );
endwhile;
endif;
?>
</div>
<?php
the_posts_pagination(
array(
'prev_text' => '',
'next_text' => '',
'mid_size' => 2,
)
);
?>
</div>
</div>
and in function.php
function kbites_words_filter_archive( $query ) {
if ( ! $query->is_main_query() )
return $query;
if ( is_admin() ) {
return;
}
if ( is_post_type_archive('tutos') ) {
if (isset($_GET['getby'])) {
if ( 'cat' === $_GET['getby'] ) {
$taxquery = array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $_GET['cat'],
),
);
$query->set( 'tax_query', $taxquery );
}
}
$query->set( 'posts_per_page', 30 );
}
return $query;
}
add_action( 'pre_get_posts', 'kbites_words_filter_archive');

Archive.php isn't sorting posts from one single category

My archive page with added numeric pagination won't sort out the right category and instead shows posts from all the categories. Let's say the category is bananas (http://localhost/tkeblog/category/bananas/) and I get post from categories bananas, oranges and apples. Also the pagination system doesn't show posts with thumbail yet it works on my index.php page. What am I doing incorrectly to filter the posts by category?
<?php
if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
else { $paged = 1; }
query_posts(array(
'post_type' => 'post', // You can add a custom post type if you like
'paged' => $paged,
'posts_per_page' => 5
));
if ( have_posts() ) : the_post(); ?>
<div class="blogitem a">
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part('catalog',get_post_format()); ?>
<?php endwhile; ?>
<div class="pagination">
<?php my_pagination(); ?>
</div>
</div>
<?php else: ?>
<p>Sorry, no posts matched your criteria.</p>
<?php wp_reset_query(); ?>
<?php endif; ?>
If we take a look at the default TwentyTwentyOne Wordpress theme archive.php we can see that the archive template is just using a default loop to display all posts from all categories, not a custom query.
I believe this answers your question.
<?php
if( have_posts() ):
while( have_posts() ): the_post();
// ... template
endwhile;
else:
// ... fallback
endif; ?>
If you want to customize the default query output from the archive.php page, the best practice is to do it from your function.php page. You can use the the action hook filter pre_get_posts.
Source # https://developer.wordpress.org/reference/hooks/pre_get_posts/
Fires after the query variable object is created, but before the actual query is run. Be aware of the queries you are changing when using the pre_get_posts action. Make use of conditional tags to target the right query.
<?php
add_action( 'pre_get_posts', function ( $query ) {
if ( ! is_admin() && $query->is_archive() && $query->is_main_query() ) {
if ( get_query_var( 'post_type' ) == 'post' ) {
$query->set( 'post_type', array( 'post' ) );
$query->set( 'posts_per_page', 12 );
$query->set( 'orderby', array( 'date' ) );
$query->set( 'order', array( 'ASC' ) );
} else {
$query->set( 'posts_per_page', 6 );
$query->set( 'orderby', array( 'date' ) );
$query->set( 'order', array( 'ASC' ) );
};
};
}; ?>

Jetpack Infinite Scroll not working in Front page

I am working on a theme which has post types - 'tour'.
I am using JetPack's infinite scroll to render new posts:
function.php
function tweakjp_custom_is_support() {
$supported = current_theme_supports( 'infinite-scroll' ) && ( is_home() || is_front_page() || is_archive() || is_single());
return $supported;
}
add_filter( 'infinite_scroll_archive_supported', 'tweakjp_custom_is_support' );
function my_child_infinite_scroll_render() {
while ( have_posts() ) : the_post();
get_template_part( 'content', get_post_type());
endwhile;
}
function my_child_setup() {
add_theme_support( 'infinite-scroll', array(
'type' => 'scroll',
'container' => 'mob_infinite',
'render' => 'my_child_infinite_scroll_render',
'posts_per_page' => 2,
) );
}
add_action( 'after_setup_theme', 'my_child_setup' );
Front Page
<div class="container">
<div id="mob_infinite">
$args = array ('post_type' => 'post');
$myquery = new wp_query($args);
if($myquery->have_posts() ){
while($myquery->have_posts() ) : $myquery->the_post();
get_template_part( 'content', get_post_type() );
endwhile;
}
</div>
</div>

Implementing pagination in wp doesn't work

I'm trying to add some code to my wordpress theme to show a pagination at the bottom of the posts.
Here's my loop with the pagination:
<main id="main">
<?php
// the query
$args = array('posts_per_page' => 2 );
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) { ?>
<!-- loop -->
<?php while ( $the_query->have_posts() ) {
$the_query->the_post(); ?>
<article id="post">
<div id="thumbnail">
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail(); } ?>
</div>
<h2><?php the_title(); ?></h2>
<div class="entry">
<?php the_excerpt(); ?>
</div>
</article>
<?php } } else { ?>
<p><?php _e( 'Die Posts entsprechen nicht den Kriterien.' ); ?></p>
<?php } ?>
<!-- pagination -->
<?php
if($the_query->max_num_pages>1){?>
<p class="paged">
<?php
if ($paged > 1) { ?>
<
<?php }
for($i=1;$i<=$the_query->max_num_pages;$i++){?>
<a href="<?php echo '?paged=' . $i; ?>" <?php echo ($paged==$i)? 'class="selected"':'';?>><?php echo $i;?></a>
<?php
}
if($paged < $the_query->max_num_pages){?>
>
<?php } ?>
</p>
<?php } ?>
<!-- end pagination -->
<!-- end of the loop -->
<?php wp_reset_postdata(); ?>
</main>
When I'm looking through the source code I can't find the pagination. What am I doing wrong? Can't find an answer, no code works for me. Would be nice if someone could help me. :)
Use default WordPress pagination, here is an example:
<?php
// set the "paged" parameter
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'posts_per_page' => 5,
'paged' => $paged,
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
// the loop
while ( $the_query->have_posts() ) : $the_query->the_post();
the_title();
endwhile;
// Pagination
echo get_next_posts_link( 'Older', $the_query->max_num_pages );
echo get_previous_posts_link( 'Newer' );
// clean up after our query
wp_reset_postdata();
else:
_e( 'Sorry, no posts matched your criteria.' );
endif;
To display next and previous links automatically, just use these functions:
next_posts_link();
previous_posts_link();
https://codex.wordpress.org/Pagination could help with this. In custom loops it helps to pass in the $max_pages variable as zero for unlimited pages, so it'd look like:
next_posts_link("Older Posts", 0);
If you want the pagination to include page links, in the format < 1 2 3 > rather than just next/previous links, you can use the WordPress function paginate_links.
So for your example the code would look like this:
<?php
//query
$paged = (isset($_REQUEST['paged']) && $_REQUEST['paged'] > 0 ? $_REQUEST['paged'] : max( 1, get_query_var('paged') ));
$args = array(
'posts_per_page' => 2,
'paged' => $paged,
);
$the_query = new WP_Query( $args );
//loop
if ($the_query->have_posts()) {
while ($the_query->have_posts()) {
$the_query->the_post();
//display your post here
}
//pagination
if($the_query->max_num_pages > 1){ ?>
<div class="paged">
<?php
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => $paged,
'total' => $the_query->max_num_pages,
'prev_text' => __('« Previous'),
'next_text' => __('Next »'),
) );
?>
</div>
<?php }
} else {
echo '<p>'._e( 'Die Posts entsprechen nicht den Kriterien.' ).'</p>';
}
wp_reset_postdata();
?>

Wordpress Pagination with custom query loads same content on every page

I am using 2 custom queries to load posts on a custom Wordpress template homepage.
the first query loads 4 'sticky' posts, and the next query loads several more recent posts.
This works great and does what I need it to do, but the pagination links when clicked show the same content on every page.
For example I have 10 posts on the homepage, and when i click to page 2 - the exact same 10 posts appear again. If I click pagination to visit page 3, the same 10 posts again etc.
No matter what page I am on, the same 10 posts are listed.
I have read something about paged being required but several failed attempts to include that in my below code have broguht me here.
Any ideas appreciated!
My code...
<?php $sticky = get_option( 'sticky_posts' );
$args = array(
'posts_per_page' => 4,
'post__in' => $sticky,
'ignore_sticky_posts' => 1,
); ?>
<?php $the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
<main id="main" class="site-main" role="main">
<?php $the_query = new WP_Query( array( 'post__not_in' => get_option( 'sticky_posts' ) ) );
if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
You sure need the $paged parameter (docs). Untested but give it a go:
<?php
// $paged holds the current page offset
$paged = ( get_query_var('paged') ) ? get_query_var( 'paged' ) : 1;
/**
* - Checks if the current page is 'paged' (false on first page)
* - Remove the check if you need those sticky posts on all pages
* - I added the $paged parameter so those sticky posts will paginate
* if you decide to show them on all pages
*
*/
if( ! is_paged() ) { // Sticky posts only on first page
$sticky = get_option( 'sticky_posts' );
$args = array(
'posts_per_page' => 4,
'post__in' => $sticky,
'ignore_sticky_posts' => 1,
'paged' => $paged // ah, the page offset
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
?>
<?php the_title(); ?>">
<?php
}
} else {
?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php
}
wp_reset_postdata();
}
?>
<main id="main" class="site-main" role="main">
<?php
$args = array(
'post__not_in' => get_option( 'sticky_posts' ),
'paged' => $paged // ah, the page offset
);
// can be done via new WP_Query but today I am lazy
// also see http://codex.wordpress.org/Function_Reference/query_posts
query_posts($args);
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
get_template_part( 'content', get_post_format() );
}
} else {
get_template_part( 'content', 'none' );
}
wp_reset_query();
?>

Categories