Hi Guys i have a problem. The Post search function is working fine on first page when gone to second page it does not show the result found from first page wordpress. Is it due to the pagination can anyone help? Thanks!
Here is the code i am using right now.
<?php
global $wp_query;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$wp_query_args = [
'post_type' => 'personality-database',
'post_status' => 'publish',
'posts_per_page' => wp_is_mobile() ? 5 : 20,
'paged' => $paged
];
if (!empty($wp_query->query_vars['personality-database-category'])) {
$wp_query_args['personality-database-category'] = $wp_query->query_vars['personality-database-category'];
}
$blogs = new WP_Query($wp_query_args);
?>
<?php if ($blogs->have_posts()) : ?>
<?php /* Start the Loop */ ?>
<?php while ($blogs->have_posts()) : $blogs->the_post(); ?>
<?php
get_template_part('template-parts/content-personality-database', 'with-data');
?>
<?php endwhile; ?>
<?php
get_template_part('template-parts/pagination', 'personality-database', array('max_num_pages' => $blogs->max_num_pages));
?>
<?php else : ?>
<?php get_template_part('template-parts/content', 'none'); ?>
<?php endif; ?>
<?php $blogs->wp_reset_postdata(); ?>
Related
I created a custom loop for exclusion of sticky post from the flow. So in that loop no sticky post came, but the problem is when I went to page/2/ or page/3/ same posts are repeating.
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$rest = new WP_Query(array(
'post__not_in' => get_option('sticky_posts'),
'paged' => $paged,
));
?>
<?php if($rest->have_posts()) { ?>
<?php while($rest->have_posts()) { ?>
<?php $rest->the_post(); ?>
<?php get_template_part('template/main/main-content'); ?>
<?php } ?>
<?php } else { ?>
<p> no post available </p>
<?php } ?>
<?php the_posts_pagination(); ?>
<?php wp_reset_query(); ?>
Use this parameter in your argument : 'posts_per_page'. It will call the next records according to the page number you pass.
I tried many attempts and finally I cracked the code.
I just add
wp_reset_query();
in the second line from top.... just after opening tag of php and it worked.
I have a custom post type that I'm searching through with a custom query. I want to output a message if there's too many results (that works fine). But the problem I have is when you first goto the page and no search has been performed, I don't want that message to appear. How can I stop it appearing when no search has been performed?
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'posts_per_page' => 5,
'post_type' => 'researchdatabase',
'post_status' => 'publish',
'paged' => $paged
);
if($searchTerm != "") {
$args['s'] = $searchTerm;
}
$the_query = new WP_Query($args);
if ($the_query->have_posts()) {
$counter = 1; ?>
<p>
<b><?php echo $the_query->post_count; ?> research items</b>
<?php if($the_query->post_count > 20) { ?>
<br /><span class="research-alert"><b>Refine your search criteria to see fewer results.</b></span>
<?php } ?>
</p>
<hr />
<br />
<?php while ($the_query->have_posts()) { $the_query->the_post(); ?>
<?php // output results ?>
<?php $counter++;
} // end while
// reset post data
wp_reset_postdata();
} else {
echo 'No results';
} // end if
?>
Just change the condition:
<?php if($the_query->post_count > 20) { ?>
With:
<?php if($the_query->post_count > 20 && is_search()) { ?>
is_search() return true if any search term is sent for the current WP_Query.
"no search has been performed" equals "no search term in the query", why don't you just check search term first?
if(!empty($searchTerm)) {
$args = array(
's' => $searchTerm
'posts_per_page' => 5,
'post_type' => 'researchdatabase',
'post_status' => 'publish',
'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1
);
$the_query = new WP_Query($args);
// Your code.
}
BTW: if you want to display total count of search results, you should use $the_query->found_posts not $the_query->post_count it only count posts on first page.
I am new to wordpress. Inside the loop I want to check if the post is the first result from the query.
This is my code.
<?php
$args = array('post_type' => 'home-slider', 'posts_per_page' => 6, 'order' => 'ASC');
$query = new WP_Query( $args );
while ($query->have_posts()) : $query->the_post();
?>
<?php if ( ) { ?> // check if first post
<p>TEST</p>
<?php } else { ?>
<p>TEST123</p>
<?php } ?>
<?php
endwhile;
?>
Thanks in advance.
Try This code
First define $inc=1; out side the while loop
Then Check in while loop if($inc==1) then print what you want and increment the $inc value using $inc++;
<?php
$args = array('post_type' => 'home-slider', 'posts_per_page' => 6, 'order' => 'ASC');
$query = new WP_Query( $args );$inc=1;
while ($query->have_posts()) : $query->the_post();
?>
<?php if ($inc==1 ) {$inc++; ?> // check if first post
<p>TEST</p>
<?php } else { ?>
<p>TEST123</p>
<?php } ?>
<?php
endwhile;
?>
The WP_Query object has a current_post field, which is actually a counter, counting from 0.
<?php
$args = array('post_type' => 'home-slider', 'posts_per_page' => 6, 'order' => 'ASC');
$query = new WP_Query( $args );$inc=1;
while ($query->have_posts()) : $query->the_post();
?>
<?php if ($query->current_post==0) { ?> // check if first post
<p>TEST</p>
<?php } else { ?>
<p>TEST123</p>
<?php } ?>
<?php
endwhile;
?>
Instead of doing the check inside the loop, you could move it to the outside and query the first post after an if() statement:
<?php
$args = array('post_type' => 'home-slider', 'posts_per_page' => 6, 'order' => 'ASC');
$query = new WP_Query( $args );
?>
<?php if ($query->have_posts()) : $query->the_post(); ?>
<p>TEST</p>
<?php
endif;
while ($query->have_posts()) : $query->the_post();
?>
<p>TEST123</p>
<?php endwhile; ?>
I'm using the following PHP script to find the most recent post on the Team area of my site.
I also use a very similar one to find the most recent news entry on my home page.
To reduce the amount of repeated code (DRY), is there a way I can use a function and just pull in a specific custom post type e.g. most_recent('team'); would show the most recent post from my Team CPT.
Here's my existing code:
<?php
// find most recent post
$new_loop = new WP_Query( array(
'post_type' => 'team',
'posts_per_page' => 1,
"post_status"=>"publish"
));
?>
<?php if ( $new_loop->have_posts() ) : ?>
<?php while ( $new_loop->have_posts() ) : $new_loop->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<?php endwhile;?>
<?php else: ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
<?php
function most_recent($type) {
$new_loop = new WP_Query( array(
'post_type' => $type,
'posts_per_page' => 1,
"post_status"=>"publish"
));
if ( $new_loop->have_posts() ) {
while ( $new_loop->have_posts() ) : $new_loop->the_post();
echo '<h2>'.the_title().'</h2>';
the_content();
endwhile;
}
wp_reset_query();
}
?>
Yes, It is indeed possible.
First what you need to do is ,
Add below code to your theme's functions.php file:
function most_recent($name){
// find most recent post
$new_loop = new WP_Query( array(
'post_type' => $name,
'posts_per_page' => 1,
"post_status"=>"publish"
));
if ( $new_loop->have_posts() ) :
while ( $new_loop->have_posts() ) : $new_loop->the_post();
echo "<h2>".the_title()."</h2>";
the_content();
endwhile;
else:
endif;
wp_reset_query();
}
Now you can use it any where in your theme folder template like below:
$most_recent = most_recent('product');
echo $most_recent;
So in your case, it would be most_recent('team') or even you can use for other as well just like I did for product.
Tell me if you have any doubt.
I am working on a wordpress site wich has a custom post type called, let's say 'plops'. I only want to use this custom post type in the website. So in the index.php, I am looping through those with a WP_Query, here is the code :
<?php
$args = array( 'post_type' => 'plops', 'posts_per_page' => 30, 'orderby' => 'desc');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$meta = get_post_meta($post->ID);
?>
// my template for the post...
<?php endwhile; ?>
The thing is I want to implement an infinite scroll to this page, and for that I need to have pagination links. I tried to implement those with the next_posts_link() function, but can't make it work !
It just won't display any link, and I tried a lot of stuff, nothing seems to make it work...
if i go to mysite.com/worpress/page/2, My posts do display, but I get a 404 in firebug... weird...
Any ideas ? Would really appreciate help ! Thanks a lot in advance !
The fix below might help you - this worked for me. It combines Chris Coyier's solution here with some help found in the comments. CSS Tricks Article
//Fix homepage pagination
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} else if ( get_query_var('page') ) {
$paged = get_query_var('page');
} else {
$paged = 1;
}
$args = array('post_type' => 'custom_post_type', 'paged' => $paged );
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query( $args );
while($wp_query->have_posts()) : $wp_query->the_post();
?>
<!-- LOOP: Usual Post Template Stuff Here-->
<?php endwhile; ?>
<nav>
<?php previous_posts_link('« Newer') ?>
<?php next_posts_link('Older »') ?>
</nav>
<?php
$wp_query = null;
$wp_query = $temp; // Reset
?>
It has been a long time since this question is posted, but for anyone who is looking for a way to achieve this in his/her own code:
$args = array( 'post_type' => 'plops', 'posts_per_page' => 30, 'order' => 'desc');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) {
$loop->the_post();
$meta = get_post_meta($post->ID);
// my template for the post...
}
previous_posts_link('« Newer');
next_posts_link('Older »', $loop->max_num_pages);
wp_reset_postdata();
Note: You have to pass the max number of pages to the next_posts_link function only.