Why does posts_per_page doesn't work in Wordpress? - php

I'm trying to take 12 posts per a page, but it displays only 4 posts.
Has anyone encountered this problem?
Current Code
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'post',
'posts_per_page' => 12,
'order' => 'DESC',
'paged' => $paged,
);
$lists = new WP_Query($args);
echo $lists->post_count; // this outputs 12!
<ul>
<?php if ($lists->have_posts()) : ?>
<?php while ($lists->have_posts()) : $lists->the_post(); ?>
<li>
<div>
<?php the_title(); ?>
</div>
</li>
<?php endwhile; ?>
<?php endif;
wp_reset_postdata(); ?>
</ul>

Please add wp_reset_query(); above $lists = new WP_Query($args);
wp_reset_query();
$lists = new WP_Query($args);

Related

How can I show specific posts on a WordPress page?

I have a page where I want to reference the titles from specific posts. This is my code with a loop right now -
<?php
$args = array(
'post_type' => 'post',
'order' => 'ASC',
'cat' => '3',
);
$product_posts = get_posts( $args );?>
<p>
<?php foreach ( $product_posts as $post ) : setup_postdata( $post ); ?>
<?php echo get_the_title(); ?>
</p>
<?php endforeach; ?>
I don't want to loop through every post though, I want to be able to single out certain posts. For example, where I have the <p> get_the_title </p> I want to be able to display it like -
<p>Title of Post 5 vs Title of Post 6</p>
How can I do this?
You can try with below:
On the $catquery query the cat=3 is category ID so you can change with your specific category ID. And post_per_page=5 is total count of post so also you can change as per your required.
<?php $catquery = new WP_Query( 'cat=3&posts_per_page=5' ); ?>
<?php while($catquery->have_posts()) : $catquery->the_post(); ?>
<p><?php the_title(); ?></p>
<?php endwhile; wp_reset_postdata(); ?>
Thanks and let me know if any query.
Try this with post ID
$postid= array(144, 246);
$args = array(
'post_type' => 'post',
'order' => 'ASC',
'post__in' => $postid,
'posts_per_page'= 5
);
// The Query
$the_query = new WP_Query( $args );
<?php while($the_query->have_posts()) : $the_query->the_post(); ?>
<p><?php the_title(); ?></p>
<?php endwhile; wp_reset_postdata(); ?>

How can I show my index page posts with post meta?

I try to show my posts in my index page with post meta for this reason I make a wp query like this:
<?php $recent = new WP_Query(
array(
'posts_per_page' => 5,
'post_type' => 'post',
'meta_key' => 'post-filter-select',
'meta_value' => 'music'));
while($recent->have_posts()) : $recent->the_post();?>
It's great and work but for pagination I have big problem! It don't work!
may I customize my index page query? How can I do this customization with keeping my pagination work ?
Quick example:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'posts_per_page' => 5,
'paged' => $paged,
'post_type' => 'page',
'meta_value' => 'music'
);
$recent = new WP_Query($args);
?>
<?php if ($recent->have_posts()) : while ($recent->have_posts()) : $recent->the_post(); ?>
// loop
<?php endwhile; endif; ?>
<nav>
<ul>
<li><?php previous_posts_link( '« PREV', $recent->max_num_pages) ?></li>
<li><?php next_posts_link( 'NEXT »', $recent->max_num_pages) ?></li>
</ul>
</nav>
<?php wp_reset_query(); ?>
Hope help you.

post_type how to get 1 title

I have done some research on this and couldnt find anything about it.
I got 4 different post.
$args = array(
'post_type'=> 'post',
'order' => 'ASC' );
$the_query = new WP_Query( $args );
if($the_query->have_posts() ) : while $the_query->have_posts() ) : $the_query->the_post();
the_title();
endwhile;
endif;
wp_reset_postdata();
this code shows all the titels, the output is like this :
1post2post3post4post
1post2post3post4post
1post2post3post4post
1post2post3post4post
but what i want is this:
1post
2post
3post
4post
How do i get this?
EDIT: Made some changes with the help of #Vincent. How do i get this 2 parts of code to work with each other?
<?php
$args = array(
'post_type'=> 'cubeportfolio',
'order' => 'ASC'
);
$the_query = new WP_Query( $args );
if($the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
?>
<?php
the_permalink();
endwhile;
endif;
wp_reset_postdata();
?>
<?php
$args = array(
'post_type'=> 'post',
'order' => 'ASC'
);
$the_query = new WP_Query( $args );
if($the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
?>
<?php
echo get_the_title()."<br/>";
endwhile;
endif;
wp_reset_postdata();
?>
After Edits - try this code
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 10,
'order' => 'ASC'
);
$the_query = new WP_Query($args);
if ($the_query->have_posts()) :
?>
<ul>
<?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
<li>
<?php the_title(); ?>
</li>
<?php
endwhile;
wp_reset_postdata
?>
</ul>
<?php endif; ?>
<?php wp_reset_postdata(); ?>
EDIT : What to you get with this part only ?
<?php
$args = array(
'post_type'=> 'post',
'order' => 'ASC'
);
$the_query = new WP_Query( $args );
if($the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<a href="'.get_the_permalink().'" >'.get_the_title().'</a>'.'<br/>';
endwhile;
endif;
wp_reset_postdata();
?>

How to get child posts of current custom post and order it by custom field number?

I have custom post type 'cars' and its child post type is 'carvariants'.
What I want to do is get child posts (carvariants) of current post (cars). I tried this code:
<div>
<?php
$parent_id = 1064;
$the_query = new WP_Query(array(
'post_parent' => $parent_id,
'post_type' => 'carvariants',
'posts_per_page' => 1,
'meta_key' => 'wpcf-minimum-price',
'orderby' => 'meta_value_num',
'order' => 'ASC'
));
?>
<?php if( $the_query->have_posts() ): ?>
<ul>
<?php while( $the_query->have_posts() ) : $the_query->the_post();
$compprd = get_the_ID(); ?>
<?php the_title(); ?>
<?php
endwhile; ?>
</ul>
<?php endif; ?>
<?php wp_reset_query(); ?>
</div>
I want to display child posts of Cars order by custom field wpcf-minimum-price
but 'post_parent' is not working. This code is showing blank output. Whats wrong in this?
I didn't try this. But I hope this will work.
If it will not work, leave me a comment, and I will try to make it work.
Also, if there are better solutions, I will be glad to see the code from professionals:
<div>
<?php
$parent_id = 1064;
$args = array( 'child_of' => $parent_id );
$children_pages = get_pages( $args );
if ( count( $children_pages ) != 0 ) :
foreach ( $children_pages as $children_page ) :
if ( $children_page->have_posts() ) :
$args_for_posts = array( 'posts_per_page' => 1,
'post_type' => 'carvariants',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'post_parent' => $children_page );
$postlist = get_posts( $args_for_posts );
foreach ( $postlist as $post) :
setup_postdata( $post ); ?>
<ul>
<?php
the_post();
?>
</ul>
<?php
endforeach;
wp_reset_postdata();
endif;
endforeach;
else : ?>
<p>No content to show.</p>
<?php
endif; ?>
</div>

Multiple custom post loops on page

I'm unable to get a second or third loop to display after the first loop is displayed. I've been trying different things for the past two hours but I'm running out of ideas.
This is what I have right now --
<?php
$type = 'new_trucks';
$args=array(
'orderby' => 'rand',
'post_type' => $type,
'paged' => $paged,
'posts_per_page' => 3,
);
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query($args);
?>
<?php while (have_posts()) : the_post(); ?>
...
<?php endwhile; /* End loop */ ?>
<?php wp_reset_query(); ?>
The second loop is like so and is right under the first loop shown above...
<?php
$type = 'used_trucks';
$args=array(
'orderby' => 'rand',
'post_type' => $type,
'paged' => $paged,
'posts_per_page' => 3,
);
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query($args);
?>
<?php while (have_posts()) : the_post(); ?>
...
<?php endwhile; /* End loop */ ?>
<?php wp_reset_query(); ?>
In my attempts to duplicate that in the same page with a different type set, the second loop is not displaying at all. If I erase the first loop, it works. I'm hoping someone can please provide some guidance.
Thanks!
<?php
$type = 'new_trucks';
$args=array(
'orderby' => 'rand',
'post_type' => $type,
'paged' => $paged,
'posts_per_page' => 3,
);
$wp_query = null;
$wp_query = new WP_Query($args);
?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
...
<?php endwhile; /* End loop */ ?>
<?php wp_reset_query(); ?>
is how I have done it in the past

Categories