I'm having troubles displaying lists of custom_post_type on my website.
I can get the posts but what is not working is "post_per_page". It gets ignored and I get every post of that type.
Here is the code I have on my home page (it's not working elsewhere too):
<?php
// 1ST
$args = array(
'post_type' => 'custom_post_1',
'post_per_page' => 1,
);
$custom_post_1 = new WP_Query($args);
if ($custom_post_1->have_posts()) : while ($custom_post_1->have_posts()) : $custom_post_1->the_post(); ?>
// My content
<?php endwhile; endif; wp_reset_postdata(); ?>
<?php
// 2ND
$args = array(
'post_type' => 'custom_post_2',
'post_per_page' => 3,
);
$custom_post_2 = new WP_Query($args);
if ($custom_post_2->have_posts()) : while ($custom_post_2->have_posts()) : $custom_post_2->the_post(); ?>
// My content
<?php endwhile; endif; wp_reset_postdata(); ?>
<?php
// 3RD
$args = array(
'post_type' => 'custom_post_3',
'post_per_page' => 1,
);
$custom_post_3 = new WP_Query($args);
if ($custom_post_3->have_posts()) : while ($custom_post_3->have_posts()) : $custom_post_3->the_post(); ?>
// My content
<?php endwhile; endif; wp_reset_postdata(); ?>
Thanks in advance!
posts_per_page and not post_per_page my friend
Please refer to the pagination parameter of WP_Query, as per that it will be posts_per_page instead post_per_page.
https://codex.wordpress.org/Class_Reference/WP_Query#Pagination_Parameters
So as per that your query will be as follows,
$args = array(
'post_type' => 'custom_post_1',
'posts_per_page' => 1,
'post_status' => 'publish',
);
$custom_post_1 = new WP_Query($args);
Hope this one helps :)
Related
Im having trouble trying to find a solution to display the child posts in a custom post type on the custom post type's archive page.
<?php wp_reset_postdata();
// WP_Query arguments
$args = array (
'post_type' => 'locations',
'posts_per_page' => -1,
);
// The Query
$location_query = new WP_Query( $args );
?>
<?php if ( $location_query->have_posts()): ?>
<ul>
<?php while ( $location_query->have_posts() ) : $location_query->the_post();
$location = get_field('map');
?>
<li>
<?php echo $location['address']; ?>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php wp_reset_postdata(); ?>
You need to add the post_parent variable in your args. Though with only that code I can't see what you'd need to do to reference the post_id.
$args = array(
'post_parent' => $post->ID,
'posts_per_page' => -1,
'post_type' => 'locations'
);
I am new to wordpress, I have a post that has a category_name of offer and it has a content, here is the permalink : http://localhost/jcjohn/2016/09/20/what-we-offer/
Now I want to display the contents of my post from my section page.
Here is my code inside the section :
<section id = "offer">
<?php
$args = array(
'type' => 'post',
'posts_per_page' => 3,
'category_name' => 'offer',
);
$offerBlog = new WP_Query($args);
if ($offerBlog->have_post()):
while ($offerBlog->have_posts()):
$offerBlog->the_post();
get_template_part('content', get_post_format());
endwhile;
endif;
wp_reset_postdata();
?>
</section>
So here is what you would need to do to display the post on the single page. You seem to have missed the s from have_post so it needs to be like below
Note: This would go inside index.php
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 3,
'category_name' => 'offer',
);
$offerBlog = new WP_Query( $args );
?>
<!-- Blog Article -->
<section id="blog">
<?php
if ( $offerBlog->have_posts() ) :
while ( $offerBlog->have_posts() ) : $offerBlog->the_post();
the_content();
endwhile;
else : ?>
<div class="no-content">
<h3>Well... it looks like we forgot to put content here.</h3>
</div>
<?php
endif;
wp_reset_postdata();
?>
</section>
You try using this loop with the cat_id that you have for the category you create.
query_posts( array( 'cat' => '1', 'posts_per_page' => 5, 'orderby' => 'title', 'order' => 'DESC' ) );
You can have a try of the replaced code as follows.
<section id = "offer">
<?php
$args = array(
'post_type' => 'post',
'cat' => '1',
'posts_per_page' => 5,
'orderby' => 'title',
'order' => 'DESC'
);
$offerBlog = new WP_Query($args);
if ($offerBlog->have_posts()):
while ($offerBlog->have_posts()):
$offerBlog->the_post();
get_template_part('content', get_post_format());
endwhile;
endif;
wp_reset_postdata();
?>
</section>
You have missed the loops format.
Reference:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
I have three loops on same page.
The first loop displays the most recent post of category "highlight".
The second loop displays others posts of this same category in chronological order.
The third loop displays all the posts, except the posts in others loops.
All works fine, but in pagination (pagenavi), the max_num_pages considers all the posts, ignoring criterias like 'post__not_in' or 'cat'.
If I use this loop, the last page of pagenavi stills in blank (counts hidden posts yet, but don't shows them):
if ($loop3->have_posts()) : while ($loop3->have_posts()) : $loop3->the_post();
And If I use this loop (loop 3), the last page of pagenavi shows the "hidden" posts:
if (have_posts()) : while (have_posts()) : the_post();
How to force the wp_query loop to exclude the hidden posts of the max_num_pages count?
//loop 1
<?php
$loop1 = new WP_query(array(
'category_name' => 'highlight',
'posts_per_page' => 1,
));
if($loop1->have_posts()) : $firstPosts = array(); while($loop1->have_posts()) : $loop1->the_post();
$firstPosts[] = $post->ID;
?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
//loop 2
<?php
$loop2 = new WP_query(array(
'post__not_in' => $firstPosts,
'category_name' => 'highlight',
'posts_per_page' => 2,
));
if($loop2->have_posts()) : while($loop2->have_posts()) : $loop2->the_post();
$firstPosts[] = $post->ID;
?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
//loop 3
<?php
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'post',
'paged' => $paged,
'post__not_in' => $firstPosts,
'cat' => -23,
);
$loop3 = new WP_Query( $args );
if ($loop3->have_posts()) : while ($loop3->have_posts()) : $loop3->the_post();
?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
//other stuff
//pagenavi
<?php wp_pagenavi("", "", array(
'query' => $loop3,
'first_text' => 'lorem ipsum',
'last_text' => 'lorem ipsum',
)); ?>
If you're using WP-PageNavi, there is a special way to use it with custom queries. For example:
$my_query = new WP_Query(
array(
'tag' => 'foo',
'paged' => get_query_var('paged')
)
);
while ( $my_query->have_posts() ) : $my_query->the_post();
the_title();
// more stuff here
endwhile;
wp_pagenavi( array( 'query' => $my_query ) );
wp_reset_postdata(); // avoid errors further down the page
So make sure you are passing your custom query object into the wp_pagenavi() function call.
~Edit~
Here's the link to the documentation: http://scribu.net/wordpress/wp-pagenavi/wpn-2-74.html
~Edit 2~
Try this code that has been customized for your particular application:
$args = array(
'post_type' => 'post',
'paged' => $paged,
'post__not_in' => $firstPosts,
'cat' => -23,
);
$loop3 = new WP_Query( $args );
...
wp_pagenavi( array( 'query' => $loop3 ) );
wp_reset_postdata();
I have created a wordpress query and I am trying to exclude one post with the id of 1293.
Here is the query I have written so far:
<?php
$my_query = new WP_Query(array (
'post__not_in' => array(1293),
'post_type' => 'product',
'posts_per_page' => '100'
));
?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
post__not in expects an array. Try the following:
$my_query = new WP_Query(array (
'post__not_in' => array(1293),
'post_type' => 'product',
'posts_per_page' => '100'
));
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