wp AJAX load more plugin repeating the same previous posts - php

I using wp ajax load more plugin While clicking the button it repeating the same previous post. How to fix it. Here I share my code in below:
<?php
$the_query = new WP_Query( array(
'posts_per_page'=>10,//on loading page i show 10 after click load more i want to show other posts
'post_type'=>'post-name',
'category_name' => 'A-E',
'orderby'=> 'title',
'order' => 'ASC',
'paged' => get_query_var('paged') ? get_query_var('paged') : 1)
);
?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
// here I print the following data
<?php
endwhile;
?>
<?php
echo do_shortcode('[ajax_load_more post_type="post-name" posts_per_page="10" category="a-e" button_label="Load More"]');
?>
can anyone fix it?

I just use this to solve my own problem:
while ( $query->have_posts() ) : $query->the_post();
$do_not_duplicate[] = $post->ID; // Store post ID in array
// Other loop actions could go here
endwhile; wp_reset_query();
$post__not_in = ($do_not_duplicate) ? implode(',', $do_not_duplicate) : '';
echo do_shortcode('[ajax_load_more post__not_in="'. $post__not_in .'" post_type="post-name" posts_per_page="10" category="a-e" button_label="Load More"]');

If you want to get the next set of pages with ajax-load-more you must use the offset parameter. The ajax-load-more plugin must get the next posts that WP_Query has. So both WP_Query and ajax-load-more must query the same pages. First alter the shortcode to be same as WP_Query, by adding order and orderby parameter:
[ajax_load_more post_type="post-name" posts_per_page="10" order="ASC" orderby="title" category="a-e" button_label="Load More"]
Then add offset=(get_query_var('paged') ? get_query_var('paged') : 1)*10 like this:
[ajax_load_more post_type="post-name" offset='.((get_query_var('paged') ? get_query_var('paged') : 1)*10).' posts_per_page="10" order="ASC" orderby="title" category="a-e" button_label="Load More"]
Note: Seems like ajax-load-more doesn't know of - so you must separate category or post_type by ,. Unless you have actually a category named a-e

You don't need to create a custom query. You are using a plugin that will generate it for you via their shortcode. As per their documentation, you can just create your settings for the shortcode.
See instructions here https://connekthq.com/plugins/ajax-load-more/docs/shortcode-builder/
Just take the generated shortcode and paste it into your page/post or in your php file with do_shortcode.
The plugin handles the query and the posts per page etc.

Related

How to fix Wordpress pagination (page not found after 8th page)

I am having trouble with Wordpress pagination. I have a custom archive page displaying custom post types from the specific category. I want to use pagination and display 12 posts per page. My problem is that pagination works correctly but only up to the 8th page. After that I am being presented with "Page not found".
I am using theme built-in function to display page navigation (1, 2... 10, 11). It correctly shows 11 pages in total, but they seem not to work after the 8th page.
$taxonomy = 'product_cat';
$term_id = get_queried_object()->term_id;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'product',
'paged' => $paged,
'posts_per_page' => '12',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $term_id
)
)
);
<?php $wp_query = new WP_query( $args ); ?>
<?php if( $wp_query->have_posts() ): ?>
<?php while( $wp_query->have_posts() ): ?>
<?php $wp_query->the_post(); ?>
//post content
<?php endwhile; ?>
<?php endif; ?>
<?php s7upf_paging_nav();?>
<?php wp_reset_postdata(); ?>
#edit
When I go to a different category page which should have 12 pages they work correctly up to the 9th page.
If I go to the one that has 2 pages, only the first one works.
I tried updaing permalinks. Setting posts per page to 12 in wordpress settings.
When i change posts per page in my query args to -1 it correctly shows all the posts on one page.
When manually setting the number of a page to display ('paged' => '11') it also displays correct page with correct posts.
You are using the wrong query. On page you are creating your own query instead of the actual query the page already did.
This will also be better for performance.
Step 1. Check what is in the normal query.
At the top of taxonomy-product_cat.php
global $wp_query;
var_dump( $wp_query->query_vars );
That probably fits mostly.
Step 2. Do the normal loop
Remove all your query stuff (maybe keep a backup of the $args for the next step)
example: Replace
<?php if( $wp_query->have_posts() ): ?>
with
<?php if( have_posts() ): ?>
And so on.
Step 3. Edit the main query
We are going to use the hook pre_get_posts
add_action('pre_get_posts', 'so_53315648');
function so_53315648( WP_Query $wp_query ){
// only check this on the main query
if (! $wp_query->is_main_query() ){
return;
}
// only check this on the product_cat taxonomy
if ( ! $wp_query->is_tax('product_cat')) {
return;
}
// maybe do some more checks?
$wp_query->query_vars['posts_per_page'] = 12;
// Is this really needed??
//$wp_query->query_vars['posts_type'] = 'product';
// tweak the query the way you like.
}
As you can see $wp_query->query_vars should pretty much be the same $args. But do not overwrite it. This might break other stuff.
Of course I could not test your specific site. But the answer should be inside the pre_get_posts hook. And tweaking the main query instead of doing a extra separate one.
Test, also check the var_dump of step 1 that your changes are coming through.
The checks at the top are to stop other pages from being affected, maybe you need more?
Let me know.

Custom tag.php template

I'm very new to PHP and have some trouble wrapping my head around it sometimes so please bear with me.
I have a lot of categories and a lot of tags. I started making category-slug.php templates but it'd probably be best for me to just use category.php and tag.php templates. I just can't get them to work unless I add in something like 'category_name' => 'art'. I've also read that querying isn't ideal (I think that's what I'm doing?), but I have had custom development done and I'm not sure if that has or hasn't been left as my only option.
$page_content = "";
if (have_posts()) :
while (have_posts()) : the_post();
$page_content .= get_the_content();
endwhile;
endif;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array( 'post_type' => 'post', 'posts_per_page' => 6, 'paged'
=> $paged );
And then later on I have this with post title, date, excerpts, etc. to follow.
<?php
$wp_query = new WP_Query($args);
while ( $wp_query->have_posts() ) : $wp_query->the_post();
?>
How do I make the category.php and tag.php pages specific to each unique slug without having to manually make each one?
Category Template Hierarchy, as described in WP codex:
The Template Hierarchy specifies that WordPress will use the first Template file it finds in your current Theme's directory from the following list:
category-slug.php
category-ID.php
category.php
archive.php
index.php
You don't have to do any custom WP_Query to get the posts of category.
if (have_posts()) :
while (have_posts()) : the_post();
the_content();//this is the content of the single post, belonging to this category
the_title();//title of the single post
the_excerpt();//excerpt of the single post
//and so on
endwhile;
endif;
It's the similar scenario for tags as well.
More details on Category Templates and Tag Templates.

Wordpress - change order by on click

Considering the following code, I have the loop in my Wordpress blog which will order posts from oldest to newer.
<?php
$args = array(
'order' => 'ASC'
);
query_posts( $args );
?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part('content'); ?>
<?php endwhile; ?>
What I want is to create two links on which the user clicks and changes this parameter, from older to newer or newer to older.
I thought about using jQuery to achieve this but I don't know exactly how I'll change the PHP code based on which link the user clicks.
If you change your sort direction to a parameter, e.g
<?php
$args = array(
'order' => (isset($_GET['dir']) ? $_GET['dir'] : 'ASC')
);
query_posts( $args );
?>
then you can create a link like:
Newest to Oldest
Oldest to Newest

WordPress - Put URL which displays all sticky posts

I am creating a new theme for my blog where I checked some posts as a Sticky post from wp-admin and on front-end I have given some CSS to highlight those sticky post.
Now I want to give link on that highlight area which redirect to particular page having all sticky posts.
I also want to do the same for other post formats as well, like IMAGE, LINK, etc.
Can someone help me on this?
You can do it with custom wordpress template and query.
Create custom page template for each post format lists page Like for sticky posts list, create page template page-sticky.php
Inside this page add custom query with loop of posts something like below :
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array( 'post_format' => 'sticky', 'posts_per_page' => 10, 'paged' => $paged );
query_posts($args);
if ( have_posts() ) : while (have_posts()) : the_post();
get_template_part( 'content', get_post_format() );
endwhile;
endif;
wp_reset_query();
You can change sticky word with any post format you want. also make sure you put everything in php quote.

Wordpress: Limit posts per page in category

In my index.php I use this code to limit the posts per page and it works without any problems:
$showposts = 5;
$do_not_show_stickies = 1;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('category__in' => $cat, 'showposts' => $showposts, 'ignore_sticky_posts' => 1, 'paged' => $paged);
$loop2query = new WP_Query($args);
query_posts($args); if(have_posts()) : while (have_posts()) : the_post(); ?>
<div class="blogpost"> ... </div>
<?php endwhile; endif;
posts_nav_link(); // Navigating the pages with $showposts each. ?>
The same code did not work in category.php so I changed it to the following but it still does not work:
$showposts = 4;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
if (have_posts()) { while (have_posts()) { the_post(); ?>
<div class="blogpost"> ... </div>
<?php } }
else { ?>
<p>There are no blog posts in this category.</p>
<?php } ?>
<?php posts_nav_link(); // Navigating the pages with $showposts each. ?>
I tried to change the line with if(have_posts()) : while (have_posts()) : the_post(); ?> [...] in category.php to make it similar to that line in index.php but nothing I tried worked.
Wordpress has a setting for this, found in the admin area under SETTINGS -> READING -> Blog pages show at most
You can use this instead of custom-modifying your queries. It may make it a little easier to maintain your project down the road.
Use the posts_per_page argument (Codex here)
$args = array('category__in' => $cat, 'posts_per_page' => $showposts, 'ignore_sticky_posts' => 1, 'paged' => $paged);
In you first example, you actually have two queries : new WP_query, then query_posts, so you need to get rid of one of them, as this is redundant. In the second exemple, it is the contrary, you do not have any query (although WordPress might execute one by default, depending on where this page is called). So anyway, there is no point in using $showposts in your 2nd example, as you are not executing a query after...
if (have_posts()) is generally used to treat a default (not visible in your page code) loop from WordPress or to treat a query that you declare just before (generally with query_posts()).
As #Samuel is saying, the argument to use is posts_per_page, but I think you are not there yet and you should first start to learn how to execute a query, so you can start by reading the WordPress codex on query_posts, it will be the best place to go first : http://codex.wordpress.org/Function_Reference/query_posts.

Categories