MPP_Media_Query class is defined in mediapress/core/common/class-mpp-media-query.php. MPP_Media_Query extends WP_Query to provide a familiar API for retrieving and listing media.
MediaPress uses an object of MPP_Media_Query internally to keep track of current page(is single media etc). MediaPress sets up the internal object on MediaPress singleton instance in setup_globals() method in MPP_Core_Component class.
Interacting with MPP_Media_Query
In general, you don’t need to worry about the internals. This class is very useful when you want to retrieve media based on certain parameters. Most of the time, you will be using MPP_Media_Query while creating a custom media loop.
Standard Loop:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | // The Query $the_media_query = new MPP_Media_Query( $args ); // The Loop if ( $the_media_query->have_media() ) { echo '<ul>'; while ( $the_media_query->have_media() ) { $the_media_query->the_media(); echo '<li>' . mpp_get_media_title() . '</li>'; } echo '</ul>'; } else { // no posts found } /* Restore original media data */ mpp_reset_media_data(); |
Another Loop:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | // the query $the_query = new MPP_Media_Query( $args ); ?> <?php if ( $the_query->have_media() ) : ?> <!-- the loop --> <?php while ( $the_query->have_media() ) : $the_query->the_media(); ?> <h2><?php mpp_get_media_title(); ?></h2> <?php endwhile; ?> <!-- end of the loop --> <?php mpp_reset_media_data(); ?> <?php else : ?> <p><?php _e( 'Sorry, no media found.' ); ?></p> <?php endif; ?> |
Whatever style you like. I recommend the second one.
Multiple Loop:-
Though it is not recommended to create many loops( because each query needs multiple database queries), If needed, you can create as many loop as you want.
Methods & properties:-
It inherits all the properties and methods from WP_Query. Other than that, It adds a few of its own.
query( $args )
It uses parent::query() and only allows limited options with query. Not all WP_Query parameters are supported by MPP_Media_Query.
get_media()
Equivalent of get_posts()
next_media()
Equivalent of next_post()
reset_next()
trackback to the previous post.
the_media()
Equivalent of the_post(). sets up current media.
have_media()
Same as have_posts()
rewind_media()
Same as rewind_posts()
is_main_query()
Are you working with the MediaPress main media query or a custom media query.
reset_media_data()
Resets current media. same as wp_reset_postdata()
paginate()
Generates pagination links for the current loop.
pagination_count()
prints the pagination count for the current loop(e.g viewing 1 to 5 of 20 photos)
get_ids()
Get an array containing all the media ids fetched by current query.
Parameters:-
Type parameters
type (array) – array of statuses e.g(array (‘photo’, ‘audio’, ‘doc’))
Component parameters:-
- component (array) – An array of componens( “members”, “groups”, “sitewide”) etc
- component_id (int) – The numeric id of component for which we are fetching the media. component must be specified.
Status parameters:-
status (array) – Array of statuses. e.g array( “public”, “private”)
Media parameters:-
- id (int) – Media Id to be fetched. You will most probably never use it as you can fetch single media using mpp_get_media()
- in (array) – An array of media ids that should be included in the result
- exclude (array) – An array of media ids to be excluded from the result
- slug (string) – A Media slug to fetch only this media
Gallery parameters:-
- gallery_id (int) – Numeric gallery id. Using it will fetch media for the given gallery.
- galleries (array) – an array of gallery ids. Only media from these galleries will be included
- galleries_exclude (array) – an array of galleries to exclude. Media from these galleries will not be fetched.
User parameters:-
- user_id (int) – Any media uploaded by this user
- user_name (string) :- Any media by this user. E.g ‘admin’
Date/Time parameters:-
- year (int) – 4 digit year(e.g. 2016)
- month (int) – 2 digit month( `1-12)
- week (int) – Week of the year( 1-53)
- day (int) – Day of the month. From 1 to 31.
See this link for all supported parameters.
Pagination parameters:-
- per_page (int) – How many items per page
- offset (int) – How many items to leave
- page (int) – Which page of the paged query
- nopaging (boolean) – Do you need to list all items
Sorting & Order parameters:-
- order (string) – Sort order. Allowed values “DESC”, “ASC”, default is “DESC”
- orderby (string) – Field to be used for sorting. It can be none, id, user, title, slug, date,modified, random, comment_count, meta_value,meta_value_num, ids. See this for more.
Search parameters:-
search_terms (string) – The string to be searched