Skip Navigation

[Resolved] Conditionally dislay the most recent CPT value only if a file exists in other CP

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

Problem:

Filter the results by custom file/image field in post view.

Solution:

It is possible with custom codes, for example:

https://toolset.com/forums/topic/conditionally-dislay-the-most-recent-cpt-value-only-if-a-file-exists-in-other-cp/#post-2210601

Relevant Documentation:

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

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 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 9 replies, has 2 voices.

Last updated by Dan 3 years, 9 months ago.

Assisted by: Luo Yang.

Author
Posts
#2206527

Dan
Screen Shot 2021-10-26 at 6.16.58 PM.png

Hi there,

I have a custom post type to display agendas and other related documents. Many CPT have been already published with future dates. I need to display one CPT date field at the top of a page only if a document has been already been uploaded on that record.

Basically, display a date field value of the most recent entry that DOES have a document.

For reference, take a look at the attached capture. At the top green section, I need to show below the "Meeting Date:" the date "October 25, 2021" as that's the last entry that does have an uploaded file.

I tried in a view:
[wpv-conditional if="( NOT(empty($(wpcf-agenda-item))) )"][types field='meeting-date' style='text' format='F j, Y'][/types][/wpv-conditional]

But this isn't working.

Any help would be appreciated.

Dan

#2207587

Hello,

Please elaborate the questions with more details:
I need to display one CPT date field at the top of a page only if a document has been already been uploaded on that record.

How do you setup the "record"? it is a custom field or repeatable field groups?

#2207597

Dan
Screen Shot 2021-10-27 at 9.15.19 PM.png
Screen Shot 2021-10-27 at 9.13.26 PM.png

Thanks for the reply, see if I can explain myself better.

The CPT has a field for a date, then, it has file uploads and text fields, a few of them; these are separate fields and non-repetitive. There are several custom posts published with future dates in the "date" custom field, but these posts are without any file upload yet.

So, what I need to display via a view, is the date of the last post that does have at least the first file upload added. Based on the image capture, the date displayed should be October 25, 2021, as that's the last record that has an uploaded file under the first file upload (then a matching text field labeled Agenda), that's the only filter/condition I need here; all other custom fields are not relevant.

My logic was to filter, in the view's main filter, by file upload, whereas if file does exist/not blank to display in my loop the date custom field. But for some reason my Views in this installation do not display "files" or "images" custom fields to filter by, which I'm not sure yet as to why they don't, but a simple filter by the file upload custom field should've resolve it? Does this make sense?

I'm adding a couple of images with hopes that they'll be of assistance.

Cheers,

Dan

#2207669

I assume you are going to add a filter to the post view:
Filter the results by custom file/image field is not empty, if it is, you can try wpv_filter_query filter hook, like this:

add_filter( 'wpv_filter_query', function($query, $setting, $view_id){
	if(in_array($view_id,array(123))){
		
		if(!isset($query['meta_query'])){
			$query['meta_query'] = array();
		}
		$query['meta_query'][] = array(
            'key'     => 'wpcf-' . 'test-file-1',
            'compare' => 'EXISTS',
        );
	}
	return $query;
}, 999, 3);

Please replace 123 with your view's ID, replace "test-file-1" with your custom file/image field slug

More help:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

#2207945

Dan

I've updated and added your code as a custom code snippet and I have just the "meeting-date" date field in the loop. The date shown now is the upcoming one "without" the file attached (November 8, 2021 in the screen capture, instead of October 25, 2021).

When you say: "Filter the results by custom file/image field is not empty" do you mean as a "View Query Filter"? If so, I cannot create any filter based on file upload custom field, none of them are available, as shown in a previous capture and not sure why, as they do show up on another installation.

What am I missing here?

Thanks,

Dan

#2208615

Q) My logic was to filter, in the view's main filter, by file upload, whereas if file does exist/not blank to display in my loop the date custom field. But for some reason my Views in this installation do not display "files" or "images" custom fields to filter by

You are right, you can not add filters on custom file/image field into post views within UI, the custom codes I provided above is for add the filters on custom file/image field into post views, filter the result which have custom file/image field value:
https://toolset.com/forums/topic/conditionally-dislay-the-most-recent-cpt-value-only-if-a-file-exists-in-other-cp/#post-2207669

Hope it is clear

#2208779

Dan

It is clear and thanks for the explanation.

I've included and updated your code and I still do not have the correct date displaying. Instead, it shows the next entry up that does not have a file uploaded.

So, to recap, this is what I have in view id 634:

Content Selection: My custom post type
Query Options: Don't include current page in query result
Ordering: Field - Agenda Ascending as a string, which is the first file upload custom field.
Limits and Offset: Mo limit skip none
Query Filter: No filter set.

Then, I updated your code replacing 123 with view id 634 and replacing test-file-1 with the file upload custom fiel slug: agenda-item.

I included your code under Toolset > Custom Code > Code snippet and activated. It runs without an error.

However, the view displays the date of the next entry that does not have a file upload; where it should show instead the previous one with the file upload.

What am I doing wring here?

Thanks,

Dan

#2209789

Since it is a custom codes problem, please provide a test site with the same problem, fill below private message box with:
1) Login credentials + FTP access
2) The problem page URL and view URL
3) Where I can edit your custom PHP codes

I need a live website to test and debug, thanks

#2210601

Thanks for the details, I have changed the PHP codes as below:

// Put the code of your snippet below this comment.
add_filter( 'wpv_filter_query', function($query, $setting, $view_id){
    if(in_array($view_id,array(634))){
         
        if(!isset($query['meta_query'])){
            $query['meta_query'] = array();
        }
        $query['meta_query'][] = array(
            'key'     => 'wpcf-' . 'agenda-item',
            'compare' => 'EXISTS',
        );
        $query['meta_query'][] = array(
            'key'     => 'wpcf-' . 'agenda-item',
            'compare' => '!=',
            'value' => '',
        );
    }
    return $query;
}, 999, 3);

It will check custom file field "agenda-item" is EXISTS and isn't empty, please test again, check if it is fixed, thanks

More help:
https://developer.wordpress.org/reference/classes/wp_query/#custom-field-post-meta-parameters

#2211379

Dan

This now works as intended. Thanks Luo for all your help, I really appreciate it.

Dan.