• I’m working on a plugin that manipulates the WordPress 4.0+ Media Library.

    I see that the attachments in the media library are loaded by an ajax call using the query-attachments action.

    I need to programatically trigger this ajax call with my own custom arguments.

    Here is a sample request that I’ve observed being sent to the server:

    Array
    (
        [action] => query-attachments
        [post_id] => 0
        [query] => Array
            (
                [orderby] => date
                [order] => DESC
                [posts_per_page] => 40
                [paged] => 2
            )
    
    )

    I would like to set the parameters myself, and then trigger this process from some custom JavaScript. Let’s say, for example, when a button is clicked – change the order_by value and send the query-attachments call to the server, repopulating the page with the results.

    How can I accomplish this?

    Please let me know if I need to provide any more details.

Viewing 2 replies - 1 through 2 (of 2 total)
  • You can modify these arguments by using the ajax_query_attachments_args-filter defined on line 2238 in /wp-admin/includes/ajax-actions.php.

    Thread Starter TylerShaw

    (@tylershaw)

    Hi tortmorten,

    I appreciate the response!

    I’m aware of that filter and it doesn’t appear that it’s going to work for this case. Using that filter allows me to modify the arguments server-side, after they’ve been received.

    However, I need to submit brand new queries for attachments from the client side of thing. So I need a method of customizing the arguments, sending the request off to the server, and then refreshing the attachments being displayed without a page reload.

    This functionality exists already, but I do not see where the code is to replicate it. For example, when you type in the search bar on the Media Library page, it fires ajax requests off to the server, reloading what attachments are shown.

    When inspecting the request in the Chrome Developer Tools, you see the following: https://puu.sh/irJHX/203ed26dd7.png (when typing the word “test” into the search bar)

    Thanks again for your response! I’ve been trying to find an answer to this for a while.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How can I manually reload the Media Library with custom arguments?’ is closed to new replies.