WordPress.org

Ready to get started?Download WordPress

Forums

Limit posts based on attachment count (8 posts)

  1. rspny
    Member
    Posted 1 year ago #

    Any ideas on how to limit the amount of posts per page based on the number of posts attachments?

    I have a custom post type archive (set up as an image grid) displaying all attachments for each post. The amount of attachments per post varies. I.e., the first 3 images are from post 1, the next 8 images are from post 2, etc.

    I'd like to limit the amount of images per page to 12 whether that results in it being 1 post or 5 posts.

  2. bcworkz
    Member
    Posted 1 year ago #

    You will need to hook into what ever is generating the images, such as the gallery shortcode, to increment an image per page counter. As soon as the counter hits 12, once the current post output is complete, output the footer and recalculate the pagination parameters so the next page consists of the next actual post instead of whatever the initial plan was.

    This precludes any real page navigation to arbitrary page numbers. I'm not sure how to even easily calculate the previous page contents short of caching the content of each page somehow, or simply relying on the browser's history. "Next" is the only viable navigation with this scheme.

    This also means the 12 is a minimum count, not maximum. For a true maximum, each post will need to pre-fetched in order to see if the image count will fit into the current page or not.

  3. Diogo15
    Member
    Posted 1 year ago #

    Looks like a challenge!

  4. rspny
    Member
    Posted 1 year ago #

    I'm uploading images as attachments within a custom post type. Each post has a varying amount of images... To make this more clear.. I'm basically creating an index of images. Some images are all from the same set. As described above, the first 3 may be from one set, while the next 8 are from another. Using the set of 3 as an example, if you were to click on any of the 3 images within the set from the index page you'd end up on a post or page that included the image you clicked as well as the other 2 from within the set.

    The issues is that I want to limit the amount of images shown per page to 12.

    I've tried removing the default post loop within the custom post type archive and then querying for the attachment-post-type (12 per page). That worked as far as the display for the first page, but when you went to the next page, it displayed the same 12 from the previous page. It did this for 5 pages (the amount I had set for my default loop's post-per-page settings within the admin).

    The reason this has become an issue is because I need each image within the grid to link back to the post it belongs to. So if the first 3 images were uploaded to the same post, all 3 would include the same link back.

  5. rspny
    Member
    Posted 1 year ago #

    @bcworkz

    Thanks for responding....

    Some of what your saying makes sense to me, but I'd definitely need 12 as the maximum.

    The good thing is I don't really need a previous page function as my original plan was to use a 1 button infinite scroll until all intervals of 12 were shown.

  6. bcworkz
    Member
    Posted 1 year ago #

    I think I have a better picture of what you're trying to do now. The parent post has little to do with any of this, other than being where the associated links go to. Your initial idea to query just the attachments is the correct approach then. You just need to iron out the pagination. With this approach, navigating back should not be a problem.

    The way I understand it now, you need exactly 12 images on each page, even if the parent post's images are split between pages. Or do all related images need to be on the same page, meaning there could be only 5 images on a page if the next parent has more than 7 images?

    I'm guessing the former, which is easier to deal with as well. Go ahead and query for attachments as you have done. If you explicitly set 'posts_per_archive_page' to 12 in the query, hopefully things will work for you. If pagination does not work right still, you may also need to pass the current page number in 'paged'. This means you'll need to track the current page yourself and use your own template tags for navigation, but it shouldn't be too onerous a task if necessary.

    The query returns an array of attachment objects, each of which contains its parent ID. You can easily create a link to the parent from the ID, though the current template tag for building links may not work. Using your own modified script to build links shouldn't be too onerous either.

    Despite Diogo15's 'skepticism', you're not really that far from a workable solution.

  7. rspny
    Member
    Posted 1 year ago #

    Thanks bcworks.

    Yes, the former is what I'm going for: 12 images per page regardless of whether or not some images from the same set carry over to the following page.

    Your solution is pretty close to what I thought might work, but because I'm only going to query by the attachment post type now, how can I query only the attachments that belong to a specific custom post type?

    Just thinking out loud, but maybe within the new loop I can get each attachments parent id to check the post type the parent belongs to. Then output only attachments that belong to the correct post type.. hmmm, sounds a little too good to be true.

  8. bcworkz
    Member
    Posted 1 year ago #

    Yes sorting in the loop will work, that's been my go to technique because I'm terrible at complex SQL queries. However, it may not scale well if there are a lot of non-related attachments, it could bog down the page performance. Additionally, it once again screws up the pagination because you cannot know how many attachments will actually meet your requirements ahead of time.

    I do believe there is a way to get a sub-query to check the parent post type so you only get related attachments, but not surprisingly, I've no idea exactly how that would be done. Shouldn't be too difficult if one knew what they were doing, as the data is all in the same table. I think this is worth pursuing further because the pagination will work much better. Unfortunately, it is outside my knowledge base.

    Try to develop the proper query as well as you can before seeking help. If you don't get the right assistance here, wordpress.stackexchange.com is another good resource.

Topic Closed

This topic has been closed to new replies.

About this Topic