Skip Navigation

[Resolved] New custom post from the last 7 days

This support ticket is created 4 years, 6 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Tagged: 

This topic contains 35 replies, has 2 voices.

Last updated by FelipeP5703 4 years, 6 months ago.

Assisted by: Shane.

Author
Posts
#1377239

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

I created a view test CPT in order to test it out.

However you can delete this once you're done 🙂

#1377249

Shane it is still not working. Rebeca, Line and Gabriela were added on the 30th of September and they should not be showing it. Look at the screenshot hidden link

Here: hidden link

All I want is to show the custom post that were published today, yesterday, yesterday - 1 .... yesterday - 6. For example, today is 6th of Nov, then posts that are added today, the 5th, the 4th .... until the 30th of Oct... then tomorrow all posts on the 30th would be removed from the list... and so on.

#1377303

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Felipe,

I've fixed this now.

I've added to the date query to limit it to the current and the last one month.

What is happening is that the date filter is matching based on the literal day and not the day, year and month.

So i've updated the filter to limit it to the past 7 days of the current and last month and the current year. There might be a case where last months items will show up if today's date is the 26th and the published date of another post is the 26th the last month.

The only way to limit this is to limit it to the current month only so that when the day repeats it doesn't show items from the previous month.

Please let me know if this helps.
Thanks,
Shane

#1377359

There might be a case where last months items will show up if today's date is the 26th and the published date of another post is the 26th the last month.

The only way to limit this is to limit it to the current month only so that when the day repeats it doesn't show items from the previous month.

Is there any other way? Because I cannot have people from last month to show up in the "new" section.

Also, what happens when the year turns? When we get to Jan 1st 2020? If this is based on the current year, no new custom post from December will show up, am I right?

#1377513

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Felipe,

Correct. This is where it will get a little tricky for us to encompass everything.

There is another workaround but how much post do you want your view to be able to display in total.

The reason is because we can set the view to display the latest 7 posts or latest 12 posts etc. This will solve the issue with the date however if there are 12 new posts created in 1 day and the view can only display 7 then only the 7 newest ones will be displayed.

Working with dates can be a bit tricky. It has to do with the fact that we have to have a choice of OR or AND comparison and not both.

If we could use both then we could say If year is 2020 or 2019 and the month is this month or 1 month in the past.

Please let me know if this clears things up for you.

Thanks,
Shane

#1377533

The thing is that we are constantly getting new clients (new custom post) thus I cannot set a fix number of posts. It's getting a bit too complicated.

What if I leave the query Published date is after or equal to: week:PAST_ONE(1), what would happen? Until what date would the post go to? And would it change next week? Meaning remove the older ones?

#1378129

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Felipe,

We will still encounter the same issue.

I'm going to have to write a complete custom php hook query for this one.

#1378139

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Felipe,

I've added the following to your custom code.

//Return only Anuncio created in the last 10 days.
add_filter( 'wpv_filter_query', 'query_recent_posts',10,3 );
 
function query_recent_posts( $query_args,$view_settings,$view_id ) {
    
  if($view_id == 4298){
   $query_args = array(
         'post_type' => 'anuncio',
    'date_query' => array(
        'after' => date('Y-m-d', strtotime('-10 days')) 
   		 )
	);  
  }
    return $query_args;
}

This is targeting the test view that I have with the ID 4298 on this page.
hidden link

It should now only return posts that were created in the last 10 days.

Please confirm and let me know if this works.

Thanks,
Shane

#1378151

Shane I appreciate the custom code but I need this as a slider. It can't be taking too much space on the page. How do I make it as a slider please?

#1378169

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Felipe,

I have a final draft of the code that should work fine now.

add_filter( 'wpv_filter_query', 'query_recent_posts',99,3 );
 
function query_recent_posts( $query_args,$view_settings,$view_id ) {
     if ( $view_id == 4298 ) {
       $query_args['date_query'] = array(
         				'column' => 'post_date_gmt',
						'after' => '10 days ago',
		);  
    }
     return $query_args;
}


I've already added this to your site so you can check currently to see if the correct posts are being returned.

Thanks,
Shane

#1378179

Shane, it seems to be working correctly. But let me see if tomorrow, the oldest post will be removed from the list and if the new ones will be added today or tomorrow, ok?

Thank you for your help so far!

#1378187

Meanwhile, any reason for the page to move like it does? What I mean is that when the content is loading the page seems to be moving up then after the content shows, it moves back down again. Anyway to keep it fixed? Do you know what I mean? I can make a video if you would like.

#1378317

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Felipe,

In response to your question here

" the oldest post will be removed from the list and if the new ones will be added today or tomorrow"

No only posts that are greater than 10 days old will be removed. If you want less time you can put it to 7. So if you create 30 posts today all 30 will show since they are within the 10 day period.

I've also checked the pages and not seeing any jumping at all. Could you record a video for this.

Thanks,
Shane

#1378453

Here is what I mean: hidden link

#1378849

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Felipe,

Thank you for the video.

I'm able to see the issue now.

What we can do is to give it a fixed height like this.


#wpv-view-layout-4298-TCPID4305{
 height:250px; 
}

This should prevent the view on the test page from auto adjusting the height.

Thanks,
Shane

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.