Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding pagination to the search #51

Open
leoloso opened this issue Mar 27, 2013 · 7 comments
Open

Adding pagination to the search #51

leoloso opened this issue Mar 27, 2013 · 7 comments

Comments

@leoloso
Copy link

leoloso commented Mar 27, 2013

Hi Jonathan,

I'm adding a new issue here to discuss the addition of pagination to the results of the search. I think this would be a wonderful thing to have.

As I explained in my previous 3 issues, I use Attachments to upload Resources to the post, like presentations, word files, or even youtube embeds (through plugin jwp6). Then, I have a page called Resources, where I select all the Resources from all the posts and display them all together. I attach an image of this page.

attachments

I would need to have pagination in function search(). However I was exploring the code, and I think it is not possible right now efficiently, because first you select the posts, and then from the metadata under metakey 'attachments' you select / filter the attachments. So let's say you want to paginate 20 results at a time, then you'd need to process all results, and then from the list just return 20.

The best way would be to already get the results, paginated, using WP_Query, so that the query wil alreadyl return the 20 values.

So I was thinking this strategy: In addition of what Attachments already does, saving the info under the post metadata under key 'attachments', also do the following:

  • save the attachment metadata not only under the post meta, but also under the attachment meta (add redundant data)
  • then in function search(), instead of querying for posts and then selecting and filtering its attached files, query directly for the attachments, like this:
$attachment_args = array(
    'post_type' => 'attachment', 
    'post_mime_type' => $filetype, 
    'post_status' => 'inherit', 
    'posts_per_page' => 20,
    'meta_query' => array(
        array(
            'key' => 'attachment_post_id',
            'value' => $post_id,
            'compare' => 'IN',
        )
    )    
);
$attachments = new WP_Query( $attachment_args );

As you can see, then you can also filter by post_mime_type already, and you can bring all results by not providing the $post_id

Also, you can add the id / title / caption / etc fields as a metadata field each and then also filter by them, for instance:

$attachment_args = array(
    'post_type' => 'attachment', 
    'post_mime_type' => $filetype, 
    'post_status' => 'inherit', 
    'posts_per_page' => 20,
    'meta_query' => array(
        array(
            'key' => 'attachment_post_id',
            'value' => $post_id,
            'compare' => 'IN',
        ),  
        array(
            'key' => 'attachment_title',
            'value' => $query,
            'compare' => 'LIKE',
        ),
        array(
            'key' => 'attachment_caption',
            'value' => $query,
            'compare' => 'LIKE',
        )
   )
);
$attachments = new WP_Query( $attachment_args );

So what do you think?

Cheers,
Leo

@jchristopher
Copy link
Owner

Thanks so much for all of your ideas and suggestions for Attachments! I've implemented them all thanks to your help but will need to think about this one. Storing redundant data would complicate the codebase quite a bit, but I understand your concern for performance. I'm going to think about this one in a bit more detail for the time being.

@leoloso
Copy link
Author

leoloso commented Apr 5, 2013

awesome! thanks to your for the plugin! :)

btw, I have my website already on a dev server, so you can see my Resources page here:

http://dev.mesym.com/en/resources/

This is the page I'd like to have paginated.

I'm mixing here with another issue, but it's handy to show it: in that same page, I added the possibility to also filter by author, I think it makes sense (in my website, the Resources are posted by users, so I display the info of who uploaded what Resource, then it makes sense to filter by author)

So I just added this:
in function search():

if( $params['author'] )
    $args['author'] = $params['author'];    // avoid using 'p' or 'page_id'

and then when I do the search:

$search_args = array(
        'instance'      => 'attachments',
        'fields'        => $fields,
        'filetypes'     => $filetypes,
        'author'        => $users
    );

This works perfectly

@leoloso
Copy link
Author

leoloso commented May 14, 2013

Hi Jonathan,

have you given a thought to this issue? I'll need to have a solution soon, I can possibly help develop it, but then of course I need you to tell me what you think is the best solution

Cheers,
Leo

@jchristopher
Copy link
Owner

Hi @leoloso, I've just released a pretty decent update to the plugin that I've been working on for a while. Now that it's out the door I can spend some more time thinking about this. It's a sticky issue though due to the reasons you've outlined, but I'll see what I can do. If you'd like to fork the repo and have a play on your end I've love to see what you come up with too!

@leoloso
Copy link
Author

leoloso commented May 14, 2013

Hi, thx for your response. It's not in my plans to fork the repo and play with it right now since I'm overloaded with work, but I wouldn't mind helping you if needed, or try to do it myself in couple of months.

If I do it myself, I still need you to tell me what you think is the best solution. Right now I'm thinking aloud: I think the best would be that when saving the properties in DB, it shouldn't use json (since then you need to iterate through all the posts to find the ones you need), but instead save them as post meta data with a non-unique key "attachment", then you can easily filter in WP_Query which posts have attachments through condition EXISTS (since WP 3.5.1)

I'm not sure though how to save the other properties (attachment caption, description, etc): also in the post metadata (each property under a different (also non-unique) key, so it is also searchable, but then I'm not sure how to match each of these properties to its corresponding attachment), or as a term, or even under a new table

@jchristopher
Copy link
Owner

Understood. Given that any solution is going to involve changes to the data store in a very significant way, I likely won't be able to finalize a strategy for some time. Given that, if you'd like to stick with Attachments I'd suggest taking the performance hit, caching the result, and paginating on your end.

@leoloso
Copy link
Author

leoloso commented May 14, 2013

good, thx for your suggestion. I'll be waiting for feedback: whenever you decide on your strategy and need help, please let me know :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants