6

I currently use query_posts to show these custom posts but I'm pretty sure that I should uses get_posts() to write it correctly.

<?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(); ?>

Thanks for your advice.

2 Answers 2

12

Hi Elium2009:

Using your code, I think this is what you were looking for? (note that WP_Query() is just the more direct version of get_posts()):

<?php $posts = 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(); ?>

Hope this helps?

3
  • THX !!!!!!!!!!!!!!!!! Exactly what I needed :)
    – Elium2009
    Nov 12, 2010 at 1:24
  • @Elium2009 - Welcome. :) Nov 12, 2010 at 8:25
  • new WP_Query(array(
    – fdrv
    Jul 13, 2020 at 19:13
0

You can use either really, but if you want to use get_posts this is how it's done:

<?php query_posts('post_type=games&posts_per_page=10'); ?>
<?php if(have_posts()) : while (have_posts() ) : the_post(); ?>
...
<?php endwhile; endif; ?>
3
  • Thanks, but I'd like to get it from my custom taxonomy associate to my custom-post, here type-mario is my taxonomy associate ton a custom-post and games his term. How would you do that ?
    – Elium2009
    Nov 11, 2010 at 22:34
  • 1
    Did you try to add the tax and term in the above code?<?php query_posts('post_type=games&posts_per_page=10&taxonomy=type_mario&term=games'); ?> <?php if(have_posts()) : while (have_posts() ) : the_post(); ?> ... <?php endwhile; endif; ?>
    – Drai
    Apr 5, 2012 at 4:00
  • From the documentation for query_posts: Note: This function will completely override the main query and isn’t intended for use by plugins or themes.
    – Gavin
    Apr 6, 2020 at 11:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy