posts_per_page is displaying 2 less items than intended - php

my first question here, so I'm sorry if I do something wrong :S
I have a custom post type, called "portfolio", and a template which I'm using to display these posts. Unfortunately, when using posts_per_page, the template is constantly displaying 2 less items than I input. Here is what I have thus far:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args= array('post_type' => 'portfolio', 'paged' => $paged,
'posts_per_page' => '4');
query_posts($args);
if( have_posts() ) :
?>
It then continues into the while loop for posting the results. Any ideas on what could be causing this?

Try to reset your query before your code.
wp_reset_query();
query_posts($args);
Function refrence: wp_reset_query()

Related

Wordpress pagination not working/displaying

Currently I am trying to get wordpress pagination to display at the bottom of the page but nothing shows up, if I echo out "paged" it outputs 1 so I believe it is correctly reading the page it is on. The loop I am using is as follows:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array( 'post_type' => 'company_list', 'orderby' => 'title', 'order' => 'ASC', 'posts_per_page' => 6, 'paged' => $paged);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
To echo out the actual pagination at the bottom of the page I am using:
echo paginate_links( $loop );
Currently no pagination is being displayed at the bottom of the page but the loop is working correctly. The page is also not set as a static front page.
Thanks.
1)You do not have to use question mark in your first row
$paged = (get_query_var('paged')) ?
2)also see :
https://stackoverflow.com/a/33757648/10210277

WordPress pagination not working with excluded posts

I'm using an array inside WP_Query, this is for the index.php page. The code is like so
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = new WP_Query( array(
'post__not_in' => $exclude_ids, // This works correctly
'paged' => $paged,
'posts_per_page' => 9, // I added this as a hope but didn't work
) );
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
but the paged is not working. I echod took the $paged out and it is doing 1,2,3 like it should (I'm assuming?) so it that part is working, and the URL is changing and the pagination link recognises it's on page 1,2,3 but the posts are all the same.
Turns out I'm dumb.
I had but the $args as new WP_Query while it should have just been the array.

Specify the first post in while loop by taxonomy

I'm trying to query posts in wordpress, so far so good. However, I would like to show a post with a specific tag 'info' in front of all (even in front of sticky).
Now the query of the theme is like this:
$sticky = get_option( 'sticky_posts' );
rsort( $sticky );
if(of_get_option('sticky-posts') == 'show_sticky'){
query_posts (array( 'post__in' => $sticky, 'order' => $order_posts, 'ignore_sticky_posts' => 1, 'paged' => $paged ) );
}
And after that the loop starts:
//BEGIN LOOP
//=====================================================?>
<article id="articlehold">
<?php
if(have_posts()) : while ( have_posts() ) : the_post();
Now I've done some editing in the query to get the post with the 'info' tag in front, but no luck. Now I'm not sure where the best place is to edit this, in the loop or before the loop?
How could I do this?

wordpress custom pagination

I have make a simple wpquery for get all events. Then with an "if" condition I check if the event start today and if yes, show the title post.
My problem is with pagination, because i don't know make a pagination based on the loop result.
if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
else { $paged = 1; }
$args = array(
'post_type' => 'events',
'posts_per_page' => '5',
'order' => 'ASC',
'paged' => $paged
);
query_posts($args);
if ( have_posts() ) while ( have_posts() ) : the_post();
$event_start = get('event_start');
// if the event start is today show the title post
if($event_start == date('d.m.Y')){
the_title();
}
endwhile;
// PROBLEM: show the pagination for all events
wp_pagenavi();
I would use a WP_Query and use the custom field parameters to only pull the relevant results. This way pagination will work directly and you won't be retrieving and looping over posts that are not required.
As a side note, you may also find this answer useful as to why you shouldn't use query_posts.

WordPress: Custom Query

I have a custom query that uses an array merge so I can list articles and custom post type posts in one list, showing 5 at a time. Pagination below allows the user to see the rest of the results. I keep getting pagination errors, but I've tried virtually every variation of the $paged variable in my query to get pagination to work and it doesn't. I know it's me, and probably a simple syntax thing...but I'm stumped. Any ideas? (Note: this page has multiple, other custom queries above the one in question)
Here's my code:
<?php
$loop1 = array_merge( $wp_query->query,
array( 'post_type' => array('post','podcasts', 'cat' => $cat_ID ),
'paged' => ( get_query_var('page') ? get_query_var('page') : 1 ),
'posts_per_page' => 5 ) );
query_posts( $loop1 );
while (have_posts()) : the_post(); ?>
have you tried?
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1 ),

Categories