Display last n images uploaded within the last 7 days


Here is a code to show last n images in member’s header, but I would like to display last n images uploaded within the last 7 days for example. I don’t know how to add a time range. I tried using wp_query with ‘date_query’ but it doesn’t work and I’m not sure if it would be possible to use it with images from rtmedia…

Hope you will help me to find a solution, Thanks a lot !

add_action( 'bp_after_member_header', 'show_last_n_images', 99 );
function show_last_n_images() {
$n = 10;
if( $user_id = bp_displayed_user_id() ) {
$model = new RTMediaModel();
$results = $model->get( array( 'media_type' => 'photo', 'media_author' => $user_id ), 0, $n );
if( $results ) {?>
<?php foreach( $results as $image ) { ?>
<div class="rtmedia-media" id="rtmedia-media-<?php echo $image->id; ?>">
<a href="<?php echo get_rtmedia_permalink( $image->id ); ?>" title="<?php echo $image->media_title; ?>">
<img src="<?php rtmedia_image( "rt_media_thumbnail", $image->id ); ?>" alt="<?php echo rtmedia_image_alt( $image->id );?>" />
<?php } ?>

Hello @marineb,

You can use rtmedia-model-where-query filter to modify or add the WHERE parameter in MySQL query.

You can find the filter here - https://github.com/rtMediaWP/rtMedia/blob/develop/app/helper/RTMediaModel.php#L111

Thank you.

Hi @pranalipatel,

Thank you very much for your help. I added the following code in functions.php and called the function “my_query_for_7_days()” in my function “show_last_n_images” but it doesn’t work. I’m not sure if I am on the right way…

function my_query_for_7_days(){
add_action('my_query_for_7_days', 'my_rtmedia_set_query_filters', 1 );

function my_rtmedia_set_query_filters() {
    add_filter( 'rtmedia_media_query', 'my_modify_media_query', 9, 3 );

function my_modify_media_query( $media_query, $action_query, $query ) {
    global $rtmedia_query;
    add_filter( 'rtmedia-model-where-query', 'my_rtmedia_model_shortcode_where_query_attributes', 10, 3 );
    return $media_query;

function my_rtmedia_model_shortcode_where_query_attributes( $where, $table_name, $join ) {
    global $rtmedia_query;
	$where .= " AND DATE(post_date) = DATE_SUB(CURDATE(), INTERVAL 30 DAY) ";
    return $where;

function my_remove_rtmedia_model_shortcode_query_attributes() {
    remove_filter( 'rtmedia-model-where-query', 'my_rtmedia_model_shortcode_where_query_attributes', 10, 3 );
add_action( 'rtmedia_before_media_gallery', 'my_remove_rtmedia_model_shortcode_query_attributes', 10, 3 ); 


Hello @marineb,

We are not sure why you have used these many functions to call the filter-function. You can try adding the filter directly to call the final function which modifies the query.

This link may help you further - https://developer.wordpress.org/reference/functions/add_filter/

Also, you are modifying the where query incorrectly. Please check database table fields to query data on a time range. You can use the upload_date field.

Thank you, Pranali

Hi @pranalipatel,

Thanks a lot for your answer, it works like a charm now with the upload_date field and the filter function.

You are welcome @marineb,

We are glad to know that your issue has been fixed.

We are closing this thread for now. Feel free to create new if you need any assistance.

Thank you, Pranali