I've got this following block of code and want to limit the number posts to 2, but it doesn't seem to work..?? I've also tried using posts_per_page, doesn't seem to work either.

Can anyone help point out where I'm going wrong?


$tax_country = strip_tags( get_the_term_list($post->ID, 'country') );

echo '<h1>More Properties in ';
echo $tax_country;
echo '...</h1>';

$relates = get_posts(array(
  'post_type' => 'propertyawardwinners',
  'numberposts' => -1,
  'tax_query' => array(
      'taxonomy' => 'country',
      'field' => 'slug',
      'terms' => array($tax_country),
      'include_children' => false

foreach ($relates as $relate) {
      echo $relate->post_title . '<br/>';
      echo  $relate->ID . '<br/>';
      echo get_field('location_city') . '<br/><br/>';


  • 1
    Simply replace 'numberposts' with 'posts_per_page' => 2 ... as you have it now, 'numberposts' => -1 will return ALL posts. Commented Apr 17, 2015 at 18:57
  • What does not working mean. not working has a million meanings. What is your exact problem Commented Apr 17, 2015 at 19:16
  • Not working means I'm changing numberposts to 2... and I still get an infinite number of posts.
    – chrsmrtn81
    Commented Apr 17, 2015 at 19:47
  • The same still happens when I use the solution from @MattVanAndel also :( ..change it to 'posts_per_page' => 2 and nothing changes
    – chrsmrtn81
    Commented Apr 17, 2015 at 19:48

1 Answer 1


Your problem is $tax_country, more actually the value. get_the_term_list() does not return what you think

Returns an HTML string of taxonomy terms associated with a post and given taxonomy. Terms are linked to their respective term listing pages.

Even if you strip away the tags, you will just be left with a string of term names. WP_Query (which is used in get_posts) do at times for some reason fails this way (returns all posts and ignores the posts_per_page value) if invalid values are passed to it.

To sort your issue, make use of wp_get_post_terms(). You can try something like the following

$tax_country = wp_get_post_terms( $post->ID, 'country', array( 'fields' => 'ids' ) );

This will return an array of term ids, so you will need to adjust your tax_query to something like this

'tax_query' => array(
        'taxonomy' => 'country',
        'terms' => $tax_country,
        'include_children' => false
  • Awesome, worked like a charm for me :) ..Thanks very much Pieter Goosen!!
    – chrsmrtn81
    Commented Apr 18, 2015 at 15:26
  • My pleasure, enjoy :-) Commented Apr 18, 2015 at 17:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.