Skip Navigation

[Resolved] Show only modified posts with updated dates

This support ticket is created 4 years, 3 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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+01:00)

This topic contains 11 replies, has 2 voices.

Last updated by Nigel 4 years, 2 months ago.

Assisted by: Nigel.

Author
Posts
#1447863

Hi team toolset!
In one of my view i need to have posts that are updated(modified) with their updated date (last modified descending).
i do not want that it shows all posts with column of post date set to ( last modified date) because in this case the post date and modified dates are same until posts is updated and i do not want such posts in the view.
i want only updated posts should be in the view with column showing their last modified date and the updated date will be greater than then posted date to get a "true" updated date.

I hope you got my problem so please provide any solution or custom code.
Regards

#1448501

Nigel
Supporter

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

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

You can use a conditional shortcode to check if the post modified date is greater than the post published date (both of which you can generate with the wpv-post-date shortcode).

Here's an example I tried quickly on my local test site which worked as expected:

		<wpv-loop>
          [wpv-conditional if="( '[wpv-post-date format='Y/m/d' type='modified']' gt '[wpv-post-date format='Y/m/d']' )"]
          <p>[wpv-post-title] ([wpv-post-date format='Y/m/d' type='modified'] | [wpv-post-date format='Y/m/d'])</p>
          [/wpv-conditional]
		</wpv-loop>

Note that I chose a format ("2020/01/07") that is amenable to comparing with "greater than".

Can you follow that to modify to your needs?

#1449399

Hi Nigel !
Yes that condition shortcut is good . i tried this in loop items here hidden link and it binged out this results hidden link.
The result of shortcode seems perfect because it bringing out all the updated posts with their updated dates.

But i want your conditional shortcode to work in my table view in such way that it shows only updated posts with all these columns hidden link .

My table hidden link should have only updated posts with their updated date there in post date column with all other custom fields information.

Please help me to have all updated posts in this table.

Thanks

#1450471

Nigel
Supporter

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

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

So you need to omit entire rows from the table when the post modified date check fails.

You probably have something like this to generate the table if you used the Loop Wizard to create the markup for table output...

	[wpv-items-found]
	<!-- wpv-loop-start -->
	<table width="100%">
		<thead>
			<tr>
				<th>[wpv-heading name="post-title"]Post title[/wpv-heading]</th>
				<th>[wpv-heading name="post-taxonomy"]Colours[/wpv-heading]</th>
				<th>[wpv-heading name="types-field-greek"]Greek[/wpv-heading]</th>
				<th>[wpv-heading name="types-field-telefono"]telefono[/wpv-heading]</th>
			</tr>
		</thead>
		<tbody class="wpv-loop js-wpv-loop">
		<wpv-loop>
			<tr>
				[wpv-post-body view_template="loop-item-in-table-of-things"]
			</tr>
		</wpv-loop>
		</tbody>
	</table>
	<!-- wpv-loop-end -->
	[/wpv-items-found]

...where the fields are added inside td tags in the linked template (the column headings are from a View in my test site).

So you would need to add the conditional shortcode around the whole row, like this in my test example:

		<wpv-loop>
		[wpv-conditional if="( '[wpv-post-date format='Y/m/d' type='modified']' gt '[wpv-post-date format='Y/m/d']' )"]
			<tr>
				[wpv-post-body view_template="loop-item-in-table-of-things"]
			</tr>
		[/wpv-conditional]
		</wpv-loop>
#1457077

I have added the short code around the whole row hidden link and it's giving fine result of updated projects sorted in last modified descending.
The problem is that after adding this shortcode it's not showing complete 50 projects according to the limit set to show 50 projects only.
i would like you to see inside if everything is settled up.

#1457663

Nigel
Supporter

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

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

OK, the problem here is that the database runs a query to get 50 posts, and then we check each of them to see whether they pass the test for the modified date, and if they fail we don't output them, so you will end up with less than the original 50 posts.

If pagination is important, you would normally need to intervene before the query so that you specify the test within the query itself, so that the query returns 50 posts that already passed the test.

The problem here is that WordPress doesn't provide for modifying the query in such a way. (You can modify the query in all sorts of other ways, but not this.)

You would have to write a custom SQL query for this, and that's not something I can help you with, I'm afraid, you would need to recruit a developer for that.

#1458803

Alright, i got your point and yes that's the reason we are getting less then 50 posts.
As my goal is to have only 50 most recently updated posts by setting limit to show 50 posts, No pagination.
If you wrap that condition in a query filter php function that executes earlier and bring out updated posts and then show us the 50 most recent updated posts out of them on our view ?

#1459569

Nigel
Supporter

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

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

The alternative is to ditch the conditional shortcode and instead run some custom code using the wpv_filter_query_post_process API hook, which fires after the query has been run, but before Views outputs them.

So you can have the View with no pagination, so all possible results are returned.

Then use that filter to manually iterate over all the results and test them to see whether the post modified date is later than the post published date, removing any posts from the results that fail that test. Then only retain the first 50 such results.

Views will then take those results to output.

Details of the API filter are here: https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query_post_process

Does that sound like a viable option?

#1462415

Yes i understand and that will be the best option.
i would like to have that conditional filter wrapped in wpv_filter_query_post_process API hook from your side, Which will first filter the posts according to condition (modified date is later than the post published date) and then we can output the latest 50 results out of them.
Looking forward for the code from your side.
Thanks

#1469047

Nigel
Supporter

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

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

Hi Andrew

Remove the conditional shortcodes from your View, and make sure the View does not include any pagination.

Then add the following PHP snippet (you can do so at Toolset > Settings > Custom Code, this code only needs to run on the front-end):

/**
 * Return only edited posts in a View query
 */
add_filter( 'wpv_filter_query_post_process', 'ts_edited_posts', 10, 3 );
function ts_edited_posts( $query, $view_settings, $view_id ){

    $applies_to = array( 123 ); // Array of View IDs to apply code to

    if ( in_array( $view_id, $applies_to ) ){

        $results = $query->posts;

        foreach ($results as $key => $result) {

            if ( $result->post_date == $result->post_modified ){
                unset($results[$key]);
            }
        }

        $results = array_slice($results, 0, 50); // limit to 50 results
        $results_count = count( $results );

        $query->posts = $results;
        $query->post_count = $results_count;
        $query->found_posts = $results_count;

    }

    return $query;
}

You will need to edit the ID of the View(s) this code applies to.

#1476867

I applied the code but its not working fine.
everything is setup according to the requirement.
No pagination. No limit. conditional code removed from the loop.
The problem comes when i do (no limit, no pagination) then the page showing related view does not work hidden link
Please take few moments and debug the problem inside. get my site access in private .
Your help will be really appreciated.

#1476871

Nigel
Supporter

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

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

What is the error?

You should be able to find details of it in your debug logs.

If you haven't already, turn on the debug log by editing your wp-config.php file and change the line with WP_DEBUG like so:

define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);
define('WP_DISABLE_FATAL_ERROR_HANDLER',true);

That will create a debug.log file in your /wp-content/ directory which you can examine in any text editor.

So, revisit the page so that it generates the error, then inspect the log.

If you share that it might be obvious to me what the problem is.

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