Skip Navigation

[Resolved] Display only the first result

This thread is resolved. Here is a description of the problem and solution.

Problem:

The issue here is that the user wanted to filter out their posts that have an empty date field.

Solution:

This can be done with the code below.

add_filter( 'wpv_filter_query', 'filter_empty_dates', 99, 3 );
    
function filter_empty_dates( $query_args,$view_settings ,$view_id ) {
       
    if ( $view_id == 503724) {
        $query_args['meta_query'] = array(
          'relation' => 'AND',
          array(
            'key' => 'wpcf-exam-date',
            'value' => '',
            'compare' => '!='
            ),
          array(
            'key' => 'wpcf-exam-date',
            'value' => time(),
            'compare' => '>='
            )
          );
    }
    return $query_args;
}

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.

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)

This topic contains 14 replies, has 3 voices.

Last updated by deepS 3 years, 9 months ago.

Assisted by: Shane.

Author
Posts
#1983675

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.

#1983923

Nigel
Supporter

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

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

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.

Is that what you mean?

#1984209

Shane
Supporter

Languages: English (English )

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

Hi Deep,

Can you provide some clarity on this as Nigel had mentioned ?

Thanks,
Shane

#1984677
Screenshot_2.jpg
Screenshot_1.jpg

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.

#1987101

Shane
Supporter

Languages: English (English )

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

Hello,

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.

Thanks,
Shane

#1988567

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.

#1989165

Shane
Supporter

Languages: English (English )

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

Hello,

Can you provide me with admin access so that I can have a look at this one for you ?

I've enabled the private fields for your next response.

Thanks,
Shane

#1990913

Shane
Supporter

Languages: English (English )

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

Hello,

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

#1991725

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.

#1992233

Shane
Supporter

Languages: English (English )

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

Hello,

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]

Please let me know if this helps.
Thanks,
Shane

#1993139

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.

#1993541

Shane
Supporter

Languages: English (English )

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

Hello,

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.

Here is the completed code version.

add_filter( 'wpv_filter_query', 'filter_empty_dates', 99, 3 );
   
function filter_empty_dates( $query_args,$view_settings ,$view_id ) {
      
    if ( $view_id == 503724) {
        $query_args['meta_query'] = array(
          'relation' => 'AND',
          array(
            'key' => 'wpcf-exam-date',
            'value' => '',
            'compare' => '!='
            ),
          array(
            'key' => 'wpcf-exam-date',
            'value' => time(),
            'compare' => '>='
            )
          );
    }
    return $query_args;
}

Please let me know if everything is working well now.

Thanks,
Shane

#1994245

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.

#1996077

Shane
Supporter

Languages: English (English )

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

Hello,

Here is the final code that combines the AND / OR logic to produce the results based on your conditionals.

add_filter( 'wpv_filter_query', 'filter_empty_dates', 99, 3 );
   
function filter_empty_dates( $query_args,$view_settings ,$view_id ) {
      
    if ( $view_id == 503724) {
        $query_args['meta_query'] = array(
          'relation' => 'AND',
          array(
            'key' => 'wpcf-exam-date',
            'value' => '',
            'compare' => '!='
            ),
          array(
            'key' => 'wpcf-exam-date',
            'value' => time(),
            'compare' => '>='
            ),
          array(
            'relation' => 'OR',
            array(
            'key'       => 'wpcf-exam-event-type',
            'value'     => 1,
            'compare'   => '=',
        	),
       		 array( 
            'key'       => 'wpcf-exam-event-type',
            'value'     => 5,
            'compare'   => '='
       		 )
          )
          );
    }
    return $query_args;
}

Please check and let me know if everything is ok now.

Thanks,
Shane

#2000195

My issue is resolved now. Thank you!