Skip Navigation

[Resolved] Split: Filtering a View by category on frontend when the Query Filter is also category. – Filter with only pdf files

This support ticket is created 3 years, 5 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 21 replies, has 2 voices.

Last updated by Luigi Bella 3 years, 4 months ago.

Assisted by: Minesh.

Author
Posts
#2141007

Minesh, you are absolutely brilliant!
Thank you so much for your help.

I had an issue on pages other than the first page of results but changing the settings from AJAX Pagination to Reload Page fixed it.

The last thing I'm trying to figure out is how I can set the output conditionally to be [pdf-embedder url='[wpv-post-url]'] if the post-url is a pdf (or contains .pdf in the url), and to display the image if it's not a pdf.

Here is an example of the issue: hidden link

#2141011

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

As you are using pagination to hide the posts that does not have PDF URL is not a solution as lets say you are on page 1 and you set the pagination to display 8 results per page and out of those 8 results 3 are not PDF files so if we check conditionally it will display only 5 results as 3 are not PDF files.

We need to filter this on another level using another filter.

I hope you got my point here.

#2141387

Apologies for the confusion, the Pagination issue is separate. Using Ajax for Pagination caused the layout to break on other pages, please disregard that first sentence.

What I need help with using Conditional Logic to make posts with ".pdf" in the URL (or just if file-type = pdf) return in the following Loop Layout:
[pdf-embedder url='[wpv-post-url]']

and all other posts are either a jpg or png, so to return those as something like:
<img src="[wpv-post-url]" alt="[wpv-post-title]" />'

Could you help me figure out how to accomplish this?

#2141781

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please share what default image you want to set - can you please share default image URL?

#2141787

Apologies if I wasn't clear.
There is no 1 image that's going to be loaded by default.
This view is for Media items and if the [wpv-post-url] of the result is a URL of a pdf I need the Loop output how it currently is, like this: [pdf-embedder url='[wpv-post-url]']

But some of the results are of jpg or PNG images, like the ones in this page: hidden link

You see that the output isn't correct because the [wpv-post-url] is not leading to PDFs but it's still in the pdf-embedder shortcode.
For these results, I need them output differently in the Loop. I think something like this should work: <img src="[wpv-post-url]" alt="[wpv-post-title]" />

The issue is that I don't know how to have a different loop output for results that are PDFs. Could you help me with that?

#2141797

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I understand that but the question is: : <img src="[wpv-post-url]" alt="[wpv-post-title]" />
- The img tag needs the image URL as source we can not assign the [wpv-post-url] as img source.

So you will have to decide if there is no PDF what image you want to display using the img tag. I hope you got my point.

#2141839

Thank you for clarifying.
I thought the [wpv-post-url] would be the url of the image.

Perhaps it would be better to only output results that are PDFs (or contain .pdf in the [wpv-post-url]). Would that be possible?

#2141841

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

If we try to check conditionally that if [wpv-post-url] has PDF or not and if it will not have PDF it will affect the pagination I mentioned with my previous reply here:
- https://toolset.com/forums/topic/split-filtering-a-view-by-category-on-frontend-when-the-query-filter-is-also-category-filter-with-only-pdf-files/#post-2141011

So, if URL does not contain the PDF we can display placeholder image and that you will need to tell me what image you want to display as placeholder image.

#2150125

Hi there,
I've simplified the requirements after discussing with my client.

What we'd like to do now is just have PDFs loaded. No other file types.
Here is the custom code already in place, maybe something could be added to this?

<?php
/**
* New custom code snippet (replace this with snippet description).
*/

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

// Put the code of your snippet below this comment.
// Filter view results
add_filter('wpv_filter_query','func_filter_view_by_tax',10,3);

function func_filter_view_by_tax($query_args, $settings, $view_id){
if($view_id == 3284318 and ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX )){

$query_args['tax_query'][] = array(
'taxonomy' => 'media_category', // taxonomy name
'field' => 'slug',
'terms' => array( 'ndpba', 'final-report','pdc-fact-sheet'),
'operator' => 'IN'
);
}
return $query_args;
}

// Remove 'ndpba', 'final-report','pdc-fact-sheet' options from view's filter dropdown
function tssupp_restrict_terms( $terms, $taxonomies, $args, $term_query ){
global $post;

// 1. pages where form (or search View) is inserted
$pages = array(3284038);

// 2. which taxonomy (slug)
$taxonomy = 'media_category';

// 3. add terms to blacklist *OR* whitelist, not both
$blacklist = array('ndpba', 'final-report','pdc-fact-sheet');
$whitelist = array();

if ( is_page( $pages ) && $taxonomies[0] == $taxonomy ) {

if ( !empty( $blacklist ) ) {

foreach( $terms as $key => $term ){

if ( in_array( $term->slug, $blacklist ) ) {
unset($terms[$key]);
}
}
} elseif ( !empty( $whitelist ) ) {

foreach( $terms as $key => $term ){

if ( !in_array( $term->slug, $whitelist ) ) {
unset($terms[$key]);
}
}
}
}

return $terms;
}
add_filter( 'get_terms', 'tssupp_restrict_terms', 10, 4 );

#2150259

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

There is no filter available based on that we can filter out only the PDF files.

Have you added any taxonomy term that when you upload PDF to post you also attach the term "PDF file" and then save the post.

Then we know that as the term attached "PDF file" - we know that this is a PDF file post and then we can filter the post.

#2150273

Thank you for your patience, I didn't know this would be so complex.

I'll add a "PDF file" category and add all pdf files to it. So I guess we'll need to edit the code above to make sure what's initially loaded is in ANY OF THE FOLLOWING: ('ndpba', 'final-report','pdc-fact-sheet') AND "PDF file"?

I was also asked by my client if we could add a second frontend filter where someone could select if they want just one of the following categories 'ndpba', 'final-report', OR 'pdc-fact-sheet' ?
This filer would work in tandem with the existing one with countries. So someone could check if there are any posts that are BOTH 'final-report', AND 'Paraguay'.

Again, thank you! You have been such a huge help.

#2150277

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Then you can simple add a frontend filter that should allow the users to filter with the terms ('ndpba', 'final-report','pdc-fact-sheet').

And you can add a custom field either radio or select field that should hold the value Is PDF: Yes - No.

If it set to yes then we can could that that post belongs is having the PDF.

If you need any help there I need problem URL and access details as we do not store admin access details.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2152259

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I'm having bit confusion as I do not know what approach you want to go with and what is your problem URL.

Can you please confirm now what approach you want to go with and what is your current issue now.

#2152343

Apologies for the confusion. I will use your suggestion for the PDF files.

Here is the URL: hidden link
What I need to do now is 2 things.

1: Add a second frontend filter(dropdown) with these 3 category options: 'ndpba', 'final-report', or 'pdc-fact-sheet'.

2. Add a search bar to the top of the view to either filter the posts below, or search the following Media Post categories and display the results on the default WordPress search results page: 'ndpba', 'final-report', or 'pdc-fact-sheet'.
For item 2, either method is fine with the client.

#2152373

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Lets solve the issue one by one.

1: Add a second frontend filter(dropdown) with these 3 category options: 'ndpba', 'final-report', or 'pdc-fact-sheet'.
==>
You can not add the same filter two times. Thats the limitation or you can say its not logical as well. That is why you will have to go with the default option which we already have the pre-filtering the view with the terms 'ndpba', 'final-report', or 'pdc-fact-sheet'.