Add the above to your Toolset custom code section in Toolset -> Custom Code and activate it. You will need to change the 503724 to your view's id as well as the wwpcf-exam-date to the slug of your image field keeping the wpcf- prefix.
This support ticket is created 3 years, 9 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.
Hi,
I have created a view for Repeatable Field Groups say various Exam events and their dates. Some have values and some are left empty as all the dates may not always be available. I hide those values by filter. But now I want to display only the first result that has proper value i.e. date. How to achieve that. Thanks.
You already worked out how to only include results that have a date set, and now you want to limit this to show only one result, with the earliest date?
You can order the results by the date field, and then set the limit option to 1, so that you only get a single result, which would be the result with the earliest date according to your ordering.
You can order the results by the date field, and then set the limit option to 1 so that you only get a single result, which would be the result with the earliest date according to your ordering.
I tried this. But it's counting the blank fields too. Hence, it doesn't display the correct results.
Please check this URL: hidden link
Please see Screenshot_1, the circled one when I have limited the display result to 1. It shows nothing. But if you inside the Exam page i.e. 'Indian Forest Service Exam 2021' (hidden link), the earliest required (filtered) date is June 27, 2021. The 'To be notified' string is showing as a condition for blank dates.
Now if I remove the limit option, it shows all the dates. See screenshot_2. Here you can see that the earliest eligible date is June 27, 2021. Hope I can make you understand. If you wish I can share login credentials too. Thanks.
Thanks for the explanation. I believe I understand what is happening here.
Some of your Date fields are empty so views is not sorting it correctly, which is expected because you can't exactly sort a null value.
What you can do is to modify the view query to exclude all the posts where the date field is empty.
This can be done using the hook below.
//Return only posts with the date field filled out.
add_filter( 'wpv_filter_query', 'filter_empty_dates', 99, 3 );
function filter_empty_dates( $query_args,$view_settings ,$view_id ) {
if ( $view_id == 44576) {
$query_args['meta_query'] = array(
array(
'key' => 'wpcf-banner',
'value' => '',
'compare' => '!='
)
);
}
return $query_args;
}
Replace wpcf-banner with the slug of your date field keeping the wpcf- prefix. Replace the 44576 with the ID of your view. Finally you will just need to add this function to the Toolset custom code section in Toolset -> Settings -> Custom Code and activate it. Once you've done this correctly then your view should no longer be including the posts with the empty date field.
Hi,
Thanks for your reply. I have added the custom code as per your instruction. But things haven't changed. It still counting the empty fields. You can have a look at this page: hidden link
I have limited the display option to 1 item.
The Exam 'Indian Forest Service Exam 2021' is not showing any date whereas it has. Please check. Thanks.
The code is actually working fine. The problem is that you have the conditional statement below.
[wpv-conditional if="( $(wpcf-exam-date) ne '') AND ( $(wpcf-exam-date) gt 'Today()') AND(( $(wpcf-exam-event-type) eq '1') OR ( $(wpcf-exam-event-type) eq '5'))"]<span style="color: #0cabd3; font-weight: 600;">[types field='exam-event'][/types]</span><br/>[types field='exam-date' style='text' format='j M Y'][/types][/wpv-conditional]
Where when I've checked the debug log for the conditional shortcode, the RFG of the "Indian Forest Service Exam 2021" post is not meeting your specified conditional.
####################
wpv-conditional attributes
####################
Array
(
[if] => ( $(wpcf-exam-date) ne '') AND ( $(wpcf-exam-date) > 'Today()') AND(( $(wpcf-exam-event-type) = '1') OR ( $(wpcf-exam-event-type) = '5'))
[debug] => true
)
####################
Debug information
####################
--------------------
Original expression: ( $(wpcf-exam-date) ne '') AND ( $(wpcf-exam-date) > 'Today()') AND(( $(wpcf-exam-event-type) = '1') OR ( $(wpcf-exam-event-type) = '5'))
--------------------
After matching 2 numeric strings into real numbers: ( $(wpcf-exam-date) ne '') AND ( $(wpcf-exam-date) > 1615939200) AND(( $(wpcf-exam-event-type) = '1') OR ( $(wpcf-exam-event-type) = '5'))
Matched '1615939200' to 1615939200
After matching 3 numeric strings into real numbers: ( $(wpcf-exam-date) ne '') AND ( $(wpcf-exam-date) > 1615939200) AND(( $(wpcf-exam-event-type) = 1) OR ( $(wpcf-exam-event-type) = '5'))
Matched '1' to 1
After matching 4 numeric strings into real numbers: ( $(wpcf-exam-date) ne '') AND ( $(wpcf-exam-date) > 1615939200) AND(( $(wpcf-exam-event-type) = 1) OR ( $(wpcf-exam-event-type) = 5))
Matched '5' to 5
--------------------
Converted expression: ( 1612915200 ne '') AND ( 1612915200 > 1615939200) AND(( 5 = 1) OR ( 5 = 5))
--------------------
The post is failing the conditional check as you can see from the Converted Expression line so nothing is being displayed in the loop even though the post itself passes the empty check from the hook i've provided.
Please let me know if this clarifies the issue for you.
Thanks,
Shane
Thanks for your insights. But I couldn't understand when I remove the display limit it shows all the result and that too even the non-blank date and its corresponding event.
Now, what are you saying there is some issue with my conditional argument. I couldn't get your point.
What my goal is to display the nearest future date and its corresponding exam event either from exam event type '1' or '5'. And of course, ignoring the blank event dates. I would like to have some suggestions/rectification in this regard. It would be really helpful for me. Thanks.
But I couldn't understand when I remove the display limit it shows all the result and that too even the non-blank date and its corresponding event.
Removing the limit will display all the date items, however not all the date items are meeting your conditional criteria. It just so happens when you are limiting the view to display only 1 item, you may end up getting a Repeatable field group that has the date filled out but doesn't meet your conditional criteria hence why you are getting the Blank output instead of the "No Posts Found".
The only way I can think of resolving this is to create another conditional to check for the date field if it is Less than today and checking if the wpcf-exam-event-type field is not equal to 1 or 5. Essentially setting up a conditional for the possible failure scenarios and display a "No Posts Found" message in the event that the post doesn't pass the criteria in your first conditional.
Your conditional would look like this.
[wpv-conditional if="( $(wpcf-exam-date) ne '') AND ( $(wpcf-exam-date) lte 'Today()') AND(( $(wpcf-exam-event-type) ne '1') OR ( $(wpcf-exam-event-type) ne '5'))"]No items Found[/wpv-conditional]
I think you are not getting my point. I want to display a future date of an exam event and not 'No items found'. In such a scenario I would like to leave it blank.
I have simplified the conditional statement a bit and the code is:
[wpv-conditional if="$(wpcf-exam-date) gte 'Today()'"][types field='exam-event'][/types]<br/>[types field='exam-date' style='text' format='j M Y'][/types][/wpv-conditional]
Here, I just want to show a date which is greater than today. And in the case of the Indian Forest Service Exam, the earliest future date is June 27, 2021. I couldn't understand whats the wrong with the logic and why it's not showing up the result. I have clearly set a condition that if the exam event date is greater than today show it. So, the count of items would naturally start where the date is greater than today.
But in the debug information I found that it is evaluating the dates smaller than today too. Why? And how to resolve the issue? Please have a look at the new conditional logic. Thanks.
I took a look at this further. Checking on the view again the main issue is the ordering. You've limited your view to display only 1 item as well as ordered it by the toolset post sort order field.
Unfortunately this has the effect of always displaying the first post in the repeatable field group. In the case of the " Indian Forest Service Exam" it is displaying the "Official notification issue date" Post which has an exam date of February 10,2021. This makes sense because it is the First post in the sort order that has a non-empty date field as per the initial custom query filter.
Given this is the case the solution is to modify the view query filter that was previously provided to get only the posts where the date field is greater than today. I've already made this change on the site so you can take a look and let me know if the correct information is now being display.
Hi Shane,
Thanks for your support. Part of my problem is solved. I simply want an addition to the custom code (as no other filter/condition is working) like the 'exam-event-type' should either be '1' or '5'.
I tried putting those conditions in the statement but they are not working.