Skip Navigation

[Gelöst] Filtering Views Query by Custom Fields

This support ticket is created vor 7 Jahre, 1 Monat. 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
- 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 14 Antworten, has 3 Stimmen.

Last updated by adamS-15 vor 7 Jahre, 1 Monat.

Assisted by: Luo Yang.

Author
Artikel
#504001

Filtering Views Query by Custom Fields

I have a custom filed (WooCommerce) with mp3 file (name_of_the_file.mp3) with some of the products that does not have an mp3 file assigned to the field (empy).

I am trying to create a filter that will render a check box

e.g
checked >> show only products with an mp3 file
unchecked >>> show all products (with and without mp3)

I think it should switch between empty field vs some_name_in_the_filed.mp3
I have tried most of the options and still don't know how to do it.

Please help.

Adam Serafin
hidden link

#504146

Minesh
Supporter

Languages: Englisch (English )

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

Hello. Thank you for contacting the Toolset support.

Well - first of all I would like to know where this custom field (mp3 file) value is stored in database - is it in postmeta table?

#504316

It's created in Post Field Groups>Product details>Audio sample

#504418

Minesh
Supporter

Languages: Englisch (English )

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

Can I have problem URL where you want to put checkbox and temporary 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 would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).

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

#504438

Minesh
Supporter

Languages: Englisch (English )

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

again - just wanted to make sure - whats your problem URL and which view you created to display such content?

#504446

I need to create this filter here > hidden link

Admin > Toolset > WordPress Archives > Product Catalog > [Edit WordPress Archive]

Thanks

🙂

#504460

Minesh,

If you could explain how to do it. Not just do it. I need to know it.

Thank you!
Adam

#505000

Minesh
Supporter

Languages: Englisch (English )

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

Ok - if you want to know yourself then you can use hook pre_get_posts:

But to add checkbox it needs custom code. Could you please check if you are able to implement the following code.

I checked as you are using WordPress archive You can user following filter:

add_action( 'pre_get_posts', 'cyb_pre_get_posts' );
function cyb_pre_get_posts( $query ) {
     
   if(is_post_type_archive('product') and $query->is_main_query() and !is_admin() ) {
		
		
		$posts_array = get_posts(array(
                        'posts_per_page' => -1,
                        'post_type' => 'product',
                        'fields' => 'ids',
                        'meta_query' => array(
                            array(
                                'key' => 'wpcf-audio-sample',
                                'compare' => 'NOT EXISTS'
                                ))));
          
                     
                    $query->query_vars['post__in'] = $posts_array;
    }
    
    
}

More help:
=> https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

#505012
screen.jpg

Hi Minesh,

Ok,

This is too complicated for me, (I don't know php)

... so are you saying that it is not possible to use toolset filter options of a custom field to check whether it's empty or not?

I was thought that this could be somehow done using the toolset otions. (attached pic)

by the way - it doesn't work > checked/unchecked shows only posts(products) with samples only.

#505177

Minesh
Supporter

Languages: Englisch (English )

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

Yes - that will work but it will prefilter your results.

I do not able to access wp-admin. What if we add checkbox field as you want and submit button, when user check the checkbox and click on submit button your view result filtered.

#505180

Minesh,

No! it didn't work! Please don't lie!

No offence but I had to recover my backup and I have removed your access to my wp-admin area and ftp.

Can I ask for someone else to help me with this problem? Please?
I need someone to answer my question in plain English and tell me how to do it and not someone who tries to make it work using php (blah blah blah)

I didn't ask you to customize my php. I only asked you to tell me if it was possible (and how / so I can understand) or not.

Thank you!

#505184

Dear Adam,

It need some custom codes, Views is using wordpress class "WP_Query" to query the posts, you can add a filter in the query, check if the custom field "mp3 file" is "NOT EXISTS", then display those posts do not have a custom field "mp3 file", see wordpress document:
https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
meta_compare (string) - Operator to test the 'meta_value'. Possible values are '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN', 'NOT EXISTS' ...

If you need more assistance for it, please provide a database dump file of your website, I can test and debug it in my localhost, and setup an example for you.

#505190
filter.jpg

Hi Luo Yang,

Possible values are '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN', 'NOT EXISTS' ...

So, probably 'NOT EXISTS' value could be used to filter out a query in my case?

But, which one of them should I use? (attached pic)

Thanks for quick reply?
Adam

#505202

There isn't "NOT EXISTS" option in the screenshot you mentioned above, this is a limitation of Views plugin, and as I mentioned above, it need custom PHP codes by adding into your theme/functions.php. please let me know if you need more assistance for it.

#505637

Luo Yang,

You're right. I thought that this could be achieved by using some combination of filtering values.
But it is not - according to what you wrote.
This could be a nice add-on for a future update to the toolset. Don't you think?

Thanks for explanation.

Adam

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