• Resolved Anonymous User 3114766

    (@anonymized-3114766)


    Hi there,

    Thanks for creating this plugin, I was wondering if there is a way to add pagination? I currently use this code snippet:

    return collect((new \WordPressPopularPosts\Query([
                    'limit'     => 10,
                    'range'     => 'monthly',
                    'order_by'  => 'views',
                    'post_type' => 'cars',
                ]))->get_posts());

    But I would like to add pagination to it so that all of the posts can be seen.

    Thanks in advance!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Hector Cabrera

    (@hcabrera)

    Hi @raphisto,

    You can use the undocumented offset parameter along with limit (which you already have) for this. It works the same way as MySQL’s OFFSET parameter.

    • This reply was modified 4 years, 4 months ago by Hector Cabrera. Reason: Fixed typo
    Thread Starter Anonymous User 3114766

    (@anonymized-3114766)

    Thanks! it works now.

    • This reply was modified 4 years, 4 months ago by Anonymous User 3114766.
    Plugin Author Hector Cabrera

    (@hcabrera)

    I would like to know beforehand how many results there are.

    Currently the Query class doesn’t know how many results were found. It wasn’t coded with pagination in mind. If you’re somewhat proficient with PHP and are familiar with WordPress development in general then you may be able to extend the Query class and/or use its internal hooks to have it calculate the total number of results.

    I’m pretty sure Laravel expects the object to expose specific methods/properties for it to be able to paginate its results. You’ll need to do some code reading and figure out what is it that Laravel needs for its native pagination function to work with WPP’s Query class and implement that yourself if you want a clean integration between WPP’s Query class and Laravel.

    Plugin Author Hector Cabrera

    (@hcabrera)

    Thanks! it works now.

    Oh, I wrote all of the above for nothing then haha.

    Might sharing how you made it work @raphisto so future readers can learn too? (and for myself as well as I’m a Laravel enthusiast.)

    Thread Starter Anonymous User 3114766

    (@anonymized-3114766)

    Hah sorry for that! I created a Collection.php as per this gist:
    https://gist.github.com/simonhamp/549e8821946e2c40a617c85d2cf5af5e

    And then used it like this:

    $collect = ((new \WordPressPopularPosts\Query([
                    'range'     => 'monthly',
                    'order_by'  => 'views',
                    'post_type' => 'cars',
    ]))->get_posts());
    
    $popular = (new Collection($collect))->paginate(config('global.perPage'), null, null, 'pages');

    Thanks again for the help!

    • This reply was modified 4 years, 4 months ago by Anonymous User 3114766.
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Is there a way to add pagination?’ is closed to new replies.