0

I've been researching this for the last 3 days. I'm triyng to limit the maximum amount of posts retrieved by get_posts().

I have this chunk of code:

// all filters should be applied here
$args = array(
    'nopaging'                  => true,
    'post_type'                 => get_option('ip_slug'),
    'posts_per_page'            => $ip_ipp,
    'limit'                     => 100,
    'orderby'                   => $ip_order,
    'order'                     => $ip_order_asc_desc,
    'author'                    => $author,
    'suppress_filters'          => false,

    'meta_key' => $ip_meta,
    'meta_query' => array(
        array(
            'key' => $ip_key,
        )
    ),
    'cache_results' => false,
    'update_post_term_cache' => false,
    'update_post_meta_cache' => false,
    'no_found_rows' => true,
);
$posts = get_posts($args);
//

The limit parameter does not work.

UPDATE #1: I want to use, say, 5 posts per page and use multiple queries on the same page using get_posts() (not query_posts, not WP_Query) and I want to limit the database query to 100. I can't use the paged parameter, due to multiple dynamically created loops.

3

You can use posts_per_page or numberposts.

http://codex.wordpress.org/Template_Tags/get_posts#Parameters

EDIT: Regarding your edit. You cannot have the posts_per_page different to the LIMIT in the SQL query. get_posts() creates an instance of WP_Query and returns it using the posts_per_page as the LIMIT.

  • posts_per_page does not limit the actual get_posts() query. Also, numberposts does not work. – Ciprian Sep 24 '14 at 13:05
  • Then why do you say in your comment that you used posts_per_page? If you are paginating you set the paged parameter to the page number and posts_per_page will limit the amount of records. There is no limit parameter in WP_Query – Jonathan Sep 24 '14 at 13:09
  • My solution is not a true WordPress one. I display 100 posts and use a jQuery pagination technique. So the question is not yet answered. I need a posts_per_page solution combined with a limit parameter. I only need get_posts() to retrieve 100 posts, so I can paginate them, I don't know, 5 or 10 per page. There is no way to limit the get_posts() query, without using the posts_per_page parameter. – Ciprian Sep 24 '14 at 13:13
  • Also, I cannot use the paged parameter, as I am using multiple queries on the same page. – Ciprian Sep 24 '14 at 13:13
  • 1
    The posts_per_page is what actually sets the LIMIT in the SQL. So, you're not making any actual sense, posts_per_page and the SQL LIMIT are the exact same thing. You cannot separate them. – Otto Sep 24 '14 at 14:25
2

The "limit" parameter does not work because there is no such parameter in get_posts or any other query in WordPress.

You can use posts_per_page to set the number of posts to retrieve, and offset to have it skip certain numbers of posts. These are the only parameters you can use, and together, they change the resulting LIMIT in the SQL query that is performed.

  • Can you give me an example of a get_posts() query for 100 posts and 5 posts per page? – Ciprian Sep 24 '14 at 14:30
  • You're confusing two different things here. The number "100" doesn't enter into it at all. You have 100 posts. You want to show five of them. You set posts_per_page to 5. End of statement. How many total posts or pages you have doesn't make any difference at all. – Otto Sep 24 '14 at 14:33
  • What if I have 10000 posts? How do I limit the query? Isn't there a parameter or a hook or a filter to modify the query? – Ciprian Sep 24 '14 at 14:46
  • 1
    There are many ways to modify the query, but my point is that you're not pulling out 10000 posts, you're only pulling them out 5 at a time. So the only value you care about is "5". How many posts there are is irrelevant, you're not getting all the posts in a single query. – Otto Sep 24 '14 at 18:47
  • Thanks Otto, I solved the problem using a different approach. See my updated answer. – Ciprian Sep 25 '14 at 8:23
0

I have solved this problem, I have used the posts_per_page parameter with a value of 100 and used jPages for jQuery pagination.

Explanation: As I needed to pull a limited number of posts - in my case, 210 - I wanted to use both pagination and a query limit. As this seems to be impossible using get_posts(), I used the posts_per_page parameter with a value of 210 and implemented a jPages pagination. Not the most elegant solution, but it does the job.

0

You have the next parameter enabled which blocks pagination, thus any limitation losts sense:

'nopaging' => true

Switch it to "disabled" or delete it. By the way, get_posts() function limits posts to 5 by default in latest WP-version. Or you can use posts_per_page attribute to directly set the limit or do it with admin panel (Settings-> Reading).

-4

You can use the pre_get_post-hook to get the number of posts that you want to display:

add_action( 'pre_get_posts', 'your_function_name' ); 
  • 1
    And how would the OP use that hook? – kaiser Sep 25 '14 at 10:37

Your Answer

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

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