How to avoid same posts in all pages - php

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.

Related

Wordpress Search Function not working on second page

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(); ?>

How to avoid duplicate post in multi

I am very new to Wordpress theme development and have found myself stuck at some point in coding.
I created two loops, where one loop has only 1 post and I styles it with bigger image, in the second loop I tried to fetch rest of the post but I am having some major problems.
In the second loop, the post of the first loop is also appearing, I want to avoid duplication.
When I go to page 2 or further, post from first loop is also appearing I want it to appear only on root page.
Below is my code.
First Loop
<?php
$bigi = array(
'posts_per_page' => '1',
'post__not_in' => get_option( 'sticky_posts' ),
);
$restp = new WP_Query($bigi);
?>
<!-- Loop started for bigimage single post -->
<?php if($restp->have_posts()) { ?>
<?php while($restp->have_posts()) { ?>
<?php $restp->the_post();
$post_id = get_the_ID(); ?>
<?php get_template_part('template/post/bigimage'); ?>
<?php } ?>
<?php } ?> <!-- Loop ended for rest of the post -->
<?php wp_reset_postdata(); ?>
Second Loop
<?php
$ropl= new WP_Query(array (
'post__not_in' => get_option('sticky_posts'),
'paged' => $paged,
));
?>
<!-- Loop started for bigimage single post -->
<?php if($ropl->have_posts()) { ?>
<?php while($ropl->have_posts()) { ?>
<?php $ropl->the_post(); ?>
<?php get_template_part('template/post/rop'); ?>
<?php } ?>
<?php } ?> <!-- Loop ended for rest of the post -->
<?php wp_reset_postdata(); ?>
This Should works. Change Query According Your Requirements.
<?php
$args1 = array('category_name' => 'test-cat-1', 'order' => 'ASC');
$q1 = new WP_query($args);
if($q1->have_posts()) :
$firstPosts = array();
while($q1->have_posts()) : $q1->the_post();
$firstPosts[] = $post->ID; // add post id to array
echo '<div class="item">';
echo "<h2>" . get_the_title() . "</h2>";
echo "</div>";
endwhile;
endif;
/****************************************************************************/
// array of post id's collected in first loop, can now be used as value for the 'post__not_in' parameter in second loops query $args
$args2 = array('post__not_in' => $firstPosts, 'order' => 'ASC' );
$q2 = new WP_query($args2);
if($q2->have_posts()) :
while($q2->have_posts()) : $q2->the_post();
echo '<div class="item">';
echo "<h2>" . get_the_title() . "</h2>";
echo "</div>";
endwhile;
endif;
?>

How to get post content from its ID in WordPress multi site?

I'm running a multi site and I want to pull post with certain categories from two blogs. Therefore I am running a loop for each of the blog to pull the posts as one of the category is in blog 1 while the other category is in blog 2.
<?php $value = array(); ?>
<?php
// Get the values from $_POST
$original_blog_id = get_current_blog_id(); // get current blog
$bids = array(1,2); // all the blog_id's to loop through EDIT
foreach($bids as $bid):
switch_to_blog($bid);
$args = array(
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'Music',
'field' => 'slug',
'terms' => array('artist', 'club')
),
)
);
$the_query = new WP_Query( $args );
?>
<?php $postids = array(); ?>
<?php if ( $the_query->have_posts() ) { ?>
<?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
<?php $postids[]=get_the_ID(); ?>
<?php endwhile; ?>
<?php $value[] = $postids; ?>
<?php
} else {
// no posts found
echo 'Nothing found.';
}
?>
<?php endforeach; ?>
<?php
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($value));
$list = iterator_to_array($it,false);
$posts = new WP_Query(array(
'post__in' => $list,
'post_type' => 'post',
));
?>
<?php
foreach ($posts as $post) {
setup_postdata($post);
?>
<?php echo the_title(); ?>
<?php
}
wp_reset_postdata(); ?>
<?php switch_to_blog($original_blog_id); ?>
The reason why I'm getting the IDs inside an array:
<?php $postids[]=get_the_ID(); ?>
because I want to fetch random post's. If at this point instead of the above statement I get the title and content of the posts then it will show in sequential order. Something like this:
BLOG1: POST1,POST2, POST : BLOG2: POST1, POST2, POST3
But I want Posts in random order like this:
BLOG1: POST1, BLOG2: POST3, BLOG1: POST2, BLOG2: POST1: BLOG2: POST2
So everything is working fine, I am able to get the posts IDs even outside the foreach loop but the problem is:
I am not able to get post content from those IDs. It only gives me posts from blog 2 because the current blog is 2. But it doesn't show anything from blog1 even though the postID is in the list array.
Can anyone please help?
The solution can be the following, just add the code into your themes footer.php files, although this can be put anywhere within your WordPress theme, example:
<?php
if (!function_exists('display_posts_from_blogs')) {
function display_posts_from_blogs($blog_id) {
global $switched;
switch_to_blog($blog_id); //switched to blog id 2, for example
// Get latest Post
$latest_posts = get_posts('category=-3&numberposts=6&orderby=post_name&order=DSC');
$cnt =0;
?>
<ul>
<?php foreach($latest_posts as $post) : setup_postdata($post);?>
<li>
<?php echo $post->post_title; ?>
</li>
<?php endforeach ; ?>
<?php restore_current_blog(); //switched back to main site
} //end function
}
?>

Display 10 blog posts on first page and 9 thereafter

I'm using the "bones" theme on WordPress. On my blog page I'm trying to have it with 9 blog posts displayed in 3 columns, but on the first page have 10 blog posts with the first (most recent) one being enlarged to span all 3 columns.
What is the best way to display 10 posts on the first page and 9 thereafter without messing up the pagination?
Here's my code: (I removed all the HTML and what-not because I assume it's not needed)
<?php if (have_posts()) : ?>
<?php $post = $posts[0]; $c=0;?>
<?php while (have_posts()) : the_post(); ?>
<?php $c++;
if( !$paged && $c == 1){
//code for the first post
} else { // THE REST: begin the code for the remainder of the posts ?>
<?php }
endif; ?>
<?php endwhile; ?>
I haven't really experimented with it as I don't have the page function set up - but try this
<?php
$post = $posts[0]; $c=0;
$c++; if( !$paged && $c == 1){
$query1 = new WP_Query( array ('posts_per_page' => 1 ) );
if ($query1-> have_posts()) : while ($query1-> have_posts()) : $query1-> the_post(); ?>
<?php the_title(); ?>
<?php endwhile; endif; wp_reset_query();}
else{
$query2 = new WP_Query( array ('posts_per_page' => 9, 'offset' => 1 ) );
if ($query2-> have_posts()) : while ($query2-> have_posts()) : $query2-> the_post(); ?>
<?php the_title(); ?>
<?php endwhile; endif; wp_reset_query(); }?>

Compare two php variables in Wordpress

I'm struggling to compare two php variables to display or not display some text depending if the variables match or not. This is what I have:
<?php $link = the_permalink();?>
<?php $portfolioloop = new WP_Query( array( 'post_type' => 'news' ) ); ?>
<?php while ( $portfolioloop->have_posts() ) : $portfolioloop->the_post(); ?>
<?php while(the_repeater_field('featured_companies')): ?>
<?php $company = the_sub_field('featured_company'); ?>
<?php if ($link == $company) { ?>
show news articles
<?php } else { ?>
don't show news articles
<?php } ?>
<?php endwhile; ?>
<?php endwhile; // end of the loop. ?>
I want to compare $link and $company and if they match then do the stuff within the if. Where am I going wrong?
I'm using the http://www.advancedcustomfields.com plugin in Wordpress if that helps.
UPDATE:
Firstly forgot to mention that the two variables are urls. At the moment it's echoing out 2 urls that are the same on the page I want, but it's also echoing out "show news articles" when the 2 urls don't match.
Underneath the twitter profile - http://www.mediwales.com/v3/members/mediwales/ shows the same two urls. But when you goto this page http://www.mediwales.com/v3/members/3m/ it shows two different urls yet shows "show news articles".
You have to be careful with Wordpress's native functions:
the_permalink() echoes out the permalink (see documentation examples)
get_permalink() returns it as a variable (see documentation examples)
So you need to be using:
$title = get_permalink();
Just solved it:
<h2>Latest News</h2>
<?php $link = get_the_title(); ?>
<?php $portfolioloop = new WP_Query( array( 'post_type' => 'news' ) ); ?>
<?php while ( $portfolioloop->have_posts() ) : $portfolioloop->the_post(); ?>
<?php $post_link = get_post_permalink(); ?>
<?php if (get_field('featured_companies') != "") { ?>
<?php foreach(get_field('featured_companies') as $post): ?>
<?php $company = get_the_title($post_object->ID); ?>
<?php if ($company == $link) { ?>
News item 1
<?php } ?>
<?php endforeach;?>
<?php } ?>
<?php endwhile; ?>

Categories