I built 2 custom posts (firemen and mario) for my template, and I built for each of them 2 taxonomy (type-mario and the term game, type-firemen and the term game) at the moment I use query_posts() for showing title of both posts linked with their term but I d'like to use get_posts() instead.
<?php query_posts( array( 'type-mario' => 'games', 'showposts' => 10 ) ); ?>
<p>Mario games</p>
<?php while ( have_posts() ) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h2><?php the_title(); ?></h2>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
<?php query_posts( array( 'type-firemen' => 'games', 'showposts' => 10 ) ); ?>
<p> Firemen Games </p>
<?php while ( have_posts() ) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h2><?php the_title(); ?></h2>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
It work well but I'm pretty sure that it's better to use get_posts() to show those 2 title posts, but I don't know how to do that.
PS: Remember that there are 2 customs posts and not classical posts, the cause of I had to build a taxonomy for each of my posts with the same term ...
Thanks for your advices.
Here is a solution:
<?php $posts = new WP_Query(array(
'taxonomy' => 'type-mario',
'term' => 'games',
'posts_per_page' => 10
)); ?>
<p>Mario games</p>
<?php while ( $posts->have_posts() ) : $posts->the_post(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h2><?php the_title(); ?></h2>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
If you've seen the documentation on
get_posts()
it functions similar toquery_posts()
.The difference between the two is that with
query_posts()
it will modify globals so that you have the use of the "the_..." global functions.With
get_posts()
it will return an array of post objects which you can loop through without affecting the current Loop if any. Additionally you can loop through multiple post sets.Note: in the WordPress example, the
setup_postdata($post)
function is used, which adds the post object as a global so that you are then able to use the "the_..." global functions (doing this however will affect The Loop).Note:
get_posts()
should take the same parameters asquery_posts()
.