I have a normal loop that outputs posts based on the given $args.
After three posts I want to insert a post that is from a Featured category. I've tried starting a new WP_Query, simple query_posts in different combinations. Nothing seems to work. Any ideas why ?
$args = array(
'post_type' => 'post',
'posts_per_page' => $count,
'paged' => $paged,
'page' => $paged,
'cat' => $cat,
'ignore_sticky_posts' => 1
);
// create a new instance of WP_Query
$my_query = new WP_Query($args);
<ul>
<?php
$i = 0;
if ($my_query->have_posts()) : while ($my_query->have_posts()) : $my_query->the_post();
if($i == 3): ?>
<li> //insert here one post from featured category
</li>
<?php endif; ?>
<li>
// the normal query stuff is here
</li>
<?php $i++ //post counter
endwhile; //end loop while
endif; //end loop
?>
</ul>
I did it this way. Before the main query I did a special query for the featured post.
$args2 = array(
'post_type' => 'post',
'posts_per_page' => 1,
'category_name' => 'TheCategoryName-Slug',
'ignore_sticky_posts' => 1
);
$my_query2 = new WP_Query($args2);
$post_ids = array(); //create an array to store the ids of the posts
if ($my_query2->have_posts()) : while ($my_query2->have_posts()) : $my_query2->the_post();
$post_ids[] = get_the_ID();
endwhile;
wp_reset_postdata();
endif;
After you have all the ids you can use them to get the data you need. Basically, all the data from the wp_posts table (http://codex.wordpress.org/Database_Description#Table:_wp_posts). The post author is returned as an id that can be used to get the name using get_userdata() function.
echo get_post_field('post_title', $post_ids[0]);
echo get_post_field('post_author', $post_ids[0]);
echo get_post_field('post_date', $post_ids[0]);
Related
In my custom post, I have 10 products and 5 categories, two products registered in each. I want to display all products and limit by category, for example, display only 1 post per category.
<?php
$args=array(
'post_type' => 'produto',
'post_status' => 'publish'
);
$my_query = null;
$my_query = new WP_Query($args);
$termsx = get_terms('categorias', $args);
foreach ($termsx as $term) :
$my_query->query( array(
'cat' => $news_cat->term_id,
'post_type' => 'produto',
'posts_per_page' => 1,
'no_found_rows' => true,
'ignore_sticky_posts' => true,
));
?>
<h2><?php echo esc_html( $news_cat->name ) ?></h2>
<?php while ( $my_query->have_posts() ) : $my_query->the_post() ?>
<div class="post">
<b><?php echo $term->name; ?></b>
<?php the_title() ?>
</div>
<?php endwhile ?>
<?php endforeach ?>
It seems to work well with the name of the terms, however the name of the post is repeating, does anyone know what is going on?
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'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 :)
I am working on a institute website which as courses.
I want to display all posts as list category wise of a custom post type.
for Ex.:
CATEGORY NAME1
- Post of category name 1
- Post of category name 1
CATEGORY NAME2
- Post of category name 2
- Post of category name 2
and so on
below is the code which is displaying all posts from custom post type from all categories but i want to show them category wise seperately please help me...
<?php
$type = 'course';
$args=array(
'post_type' => $type,
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1);
$my_query = '';
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><?php the_title(); ?></p>
<?php
endwhile;
}
wp_reset_query();
?>
you may need to do your query more than once, or one per category the key is adding 'category_name' => 'slug_name' to your query
<?php
// query category 1
$type = 'course';
$args1=array(
'post_type' => $type,
'post_status' => 'publish',
'posts_per_page' => -1,
'category_name' => 'slug_name' // added the category name enter the slug name as defined in the category
'caller_get_posts'=> 1);
// query category 2
$type = 'course';
$args2=array(
'post_type' => $type,
'post_status' => 'publish',
'posts_per_page' => -1,
'category_name' => 'slug_name' // added the category name enter the slug name as defined in the category
'caller_get_posts'=> 1);
$my_query = '';
$my_query = new WP_Query($args1);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><?php the_title(); ?></p>
<?php
endwhile;
}
wp_reset_query();
$my_query = '';
$my_query = new WP_Query($args2);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><?php the_title(); ?></p>
<?php
endwhile;
}
wp_reset_query();
?>
100% working this code
$tax_query[] = array(
'taxonomy' => '< taxonomy_slug >',
'field' => 'term_id',
'terms' => < terms_ids >, // array(12,13,...)
);
$args = array(
'post_type' => '< post type name>',
'posts_per_page' => 10,
'tax_query' => $tax_query
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo get_the_ID();
endwhile;
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();