Skip Navigation

[Resolved] Views query filter based on custom function

This support ticket is created 6 years, 7 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 7 replies, has 3 voices.

Last updated by Nigel 6 years, 7 months ago.

Assisted by: Nigel.

Author
Posts
#705099

I am trying to filter the views post listings by comparing the post's published date and the post's modified date.

I was trying to follow some previous similar support tickets already available on Toolset but could not achieve what I needed. Here are the links to those support tickets which are already resolved:
https://toolset.com/forums/topic/query-filter-by-has-featured-image-on-view/
https://toolset.com/forums/topic/creating-a-view-that-filters-by-post-type/

My objective is to filter out the posts of the post's publish date is equal to the post's modified date or where the post's modified date in within 5 minutes of the post 's published date. Thus I would only get the posts listed with do not have the post's modified date equal or within 5 minutes to the post's published date.

#705433

Set a Query Filter in Views like this:

Select posts whose
Modified date belongs to a minute not between those: VIEW_PARAM(postdate),VIEW_PARAM(postdateplus5)

Then create a ShortCode like this:

function post_date_plus_five_function( $atts ){

	$post_date = do_shortcode("[wpv-post-date format='U']");
	
	$a = shortcode_atts( 
			array(
				'postdate' => $post_date,
    		), 
		$atts 
	);

    $time_after = $a['postdate'] + 5;
    
    return $time_after;

}
add_shortcode( 'post-date-plus-five-shortcode', 'post_date_plus_five_function' );

Register it in Toolset > Settings > Frontend Content > 3rd Party ShortCodes

Then insert the View in a Page like this:

[wpv-view name="your_view" postdate="[wpv-post-date format='U']" postdateplus5="[post-date-plus-five-shortcode]"]

This should then return posts that do NOT have a Modified Date at the Time of Post date + 5 (seconds in this case).

#726128

Hi Beda,
Thank you for your prompt reply and I would say you gave me exactly what I initially conceptualized. In fact you have actually taught me how to pass arguments through wpvshortcode. Many thanks for that.

But actually I am still missing something to achieve what is required for my project. I am sure you if the above method has not worked for me, you will be able to point me to the right direction.

I'll explain you my requirement. I am trying to list the entries of a custom post type "i.e. Downloads" into two columns, first column list the posts based on their "Initial Published Date" and the second column lists them based on the "Date they were last modified".

Listing the posts based on their initial Published date is pretty straight forward, but when sorting them for the second column for the "Date they were last modified" by sorting them with their "Modified_Date" also includes the newly published posts. I am looking for a method to exclude the newly published posts form this post listing.

For this I cam up with the idea to compare the post published_date with the post modified_date and if the difference is less than 5 minutes, then such posts should be filtered out of the listing.

Somehow am not able to achieve it. I am sure you have a much better idea than comparing the dates.

Once again thank you for all the great help.

#726765

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Screen Shot 2018-04-24 at 13.21.45.png

Hi there

Beda is working on another project, so let me take over here if that's okay.

I read your update and I'm not completely sure I understand the requirement, so let me clarify that before I continue.

When you talk of two columns, I wasn't sure whether you mean displaying the results from a single View in two columns (which you could sort as required) as shown in the screenshot, or whether you meant you are displaying two Views (which happen to be displayed in columns), the first View displays all download posts (ordered by publish date) and the second View displays only those download posts which have been modified (i.e. the post modified date does not equal the publish date).

That would mean duplicates because the first column shows all posts, unless you want to exclude the posts which appear in the second column from the first.

Can you clarify the requirement and then I'll help you set it up.

#726862

Hi Nigel,
Its great to have you, you helped me very precisely with my last support ticket, will be great if you can guide me on this one.

The problem is not displaying the columns, actually I was trying to elaborate in depth, but forgive me for going into unnecessary details.

I am simply looking to display the list of posts using Views which are sorted by the modified date, but such a view is including the newly added ones as well which I do not need to be listed in this view.

I came up with the idea that filtering out the posts which have the difference of 5 minutes between the publish_date and the modified_date can solve this, but somehow the above solution given by Beda to achieve this is not still not filtering out the posts for me. I am assuming that possibly I am making conceptual mistakes in comparing both the dates or may be the above method provided by Beda requires some modification to achieve what I need.

Hope this clarifies the problem. Thanks for any help.

#728307

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

It's not very straightforward to use the WP_Query arguments to construct a query where you can compare the post_date and post_modified fields, so I suggest creating a View which will display all possible posts and then using the wpv_filter_query_post_process API filter to manipulate the results and remove the unwanted ones.

I did that with the following code:

function tssupp_modified_posts( $query, $view_settings, $view_id ){

	if ( $view_id == 105 ) {

		$query_posts = $query->posts;

		foreach ($query_posts as $key => $query_post) {

			if ( $query_post->post_date == $query_post->post_modified ) {

				// post has not been modified, remove from results
				unset( $query_posts[ $key ] );
				$query->post_count--;
				$query->found_posts--;
			}
		}

		// reindex and update results
		$query->posts = array_values( $query_posts );
	}

	return $query;
}
add_filter( 'wpv_filter_query_post_process', 'tssupp_modified_posts', 101, 3 );

It will only show posts which have been modified. You should just need to edit the view id.

https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query_post_process

#735005

Absolutely what I needed. It really works. Thanks for everything.
However I just have one small issue with that if you could help that's great if not, I'll be good with the code you have provided. The issue is that, the view that I have created, is suppose to list only 5 updated posts. Now In-case when there are only new posts being added, the 5 view returns "No post found" simply because all the last 5 posts were new additions and not the modifications. Is there a way to make this function smart and instead of counting those new additions, simply omit the results in a way that it does not affect the listing at all using this function?

Thanks a lot.

#735211

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

I updated the code to show a maximum of 5 modified posts, but I haven't tested it, I'll leave that to you.

function tssupp_modified_posts( $query, $view_settings, $view_id ){
 
    if ( $view_id == 105 ) {
 
        $query_posts = $query->posts;
 
        foreach ($query_posts as $key => $query_post) {
 
            if ( $query_post->post_date == $query_post->post_modified ) {
 
                // post has not been modified, remove from results
                unset( $query_posts[ $key ] );
            }
        }
 
        // limit to maximum of 5 results
        $limited_posts = array_splice( $query_posts, 0, 5 );

        $number_of_posts = count( $limited_posts );

		$query->post_count = $number_of_posts;
        $query->found_posts = $number_of_posts;

        // reindex and update results
        $query->posts = $limited_posts;
    }
 
    return $query;
}
add_filter( 'wpv_filter_query_post_process', 'tssupp_modified_posts', 101, 3 );

You should remove any limit on the View itself.