Skip Navigation

[Resolved] Filter by WooCommerce Post Status

This support ticket is created 6 years, 7 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 14 replies, has 2 voices.

Last updated by Nashaat 6 years, 7 months ago.

Assisted by: Luo Yang.

Author
Posts
#920610

I am trying to add a dropdwon filter for filtering by post status. I have followed this this : https://toolset.com/forums/topic/post-status-filter-select/

i have added this into my filter section in vies

[wpv-control field="my-post-status" url_param="my-post-status" type="select" values="any,publish,draft" display_values="Any,Publish,Draft"]

and this function with my views id number 437

add_filter( 'wpv_filter_query_437', 'filter_post_type_status_func', 10, 3 );
 
function filter_post_type_status_func( $query_args, $view_settings, $view_id ) {
    if ( $view_id == 437 ) {
        if(isset($_GET['my-post-status'])){
            $query_args['post_status'] = $_GET['my-post-status'];
        }
    }
    return $query_args;
}

it seems not to work.

#920765

Hello,

Please modify your PHP codes from:

add_filter( 'wpv_filter_query_437', 'filter_post_type_status_func', 10, 3 );

To:

add_filter( 'wpv_filter_query', 'filter_post_type_status_func', 10, 3 );

And test again, more help:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

#920777
Function.png
Filter.png

I have modified the code. seems not to work!

#920782
status.JPG

How does it seem not to work?
And there is one step missing, you will need to query all status posts by default, for example, you need to add a filter in the view:
Select posts with status of any.

See screenshot status.JPG

#920784
Post-Status-link.png

If i change post status in the link manually it updates the results. but the input itself show me only one option "Any" it doesn't populate draft,publish,pending etc... please see attached image

#920789

Thanks for the details, the dropdown menu is passing URL parameter in array, so you will need to change these line

From:

..
        if(isset($_GET['my-post-status'])){
            $query_args['post_status'] = $_GET['my-post-status'];
        }
...

to:

..
        if(isset($_GET['my-post-status'][0])){
            $query_args['post_status'] = $_GET['my-post-status'][0];
        }
...
#920790
Troubleshooting.png
Query Filter.png

Query Filter is assigned but still not working!
i have an error message in the Toolset debugging settings, maybe this causing the issue?

here you can see the views filter: hidden link

#920793

I have changed my code to following but still not working

add_filter( 'wpv_filter_query', 'filter_post_type_status_func', 10, 3 );
  
function filter_post_type_status_func( $query_args, $view_settings, $view_id ) {
    if ( $view_id == 437 ) {
        if(isset($_GET['my-post-status'][0])){
            $query_args['post_status'] = $_GET['my-post-status'][0];
        }
    }
    return $query_args;
}

no option showing in select filter

#920804

I tried above codes in my localhost it works fine
[wpv-control field="my-post-status" url_param="my-post-status" type="select" values="any,publish,draft" display_values="Any,Publish,Draft"]

I can see all options in the select dropdown menu:
Any,Publish,Draft

Since it is a custom codes problem, please check these in your website:
1) Deactivate other plugins and switch to wordpress default theme 2017, and test again

2) If the problem still persists, please provide a test site with the same problem, fill below private detail box with login details and ftp access, also point out where I can edit your PHP codes. I need to test and debug it in a live website. thanks

#921049

Thanks for the details:
I have modified the view "All Ads Search"
hidden link
In section "Custom Search Settings", enable option "Always show all values for inputs", test it in front-end:
hidden link

I can see those options in the dropdown menu "Post Status:"
Any,Publish,Draft

Since the "Post Status:" dropdown menu is not Views built-in feature, it does not support option " Show only available options for each input"

#921054

And I have modified your PHP codes to this:

add_filter( 'wpv_filter_query', 'filter_post_type_status_func', 99, 3 );
  
function filter_post_type_status_func( $query_args, $view_settings, $view_id ) {
    if ( $view_id == 437 ) {
        if(isset($_GET['my-post-status']) && is_array($_GET['my-post-status'])){
           $query_args['post_status'] = end($_GET['my-post-status']);
        }
    }
    return $query_args;
}

Since there is only one "Pending" post in your website, To test the result, I have modified the shortcode as below:

     [wpv-control field="my-post-status" url_param="my-post-status" type="text" values="any,publish,pending" display_values="Any,Publish,Pending"]
#921847

Yes you are right. now it works. the only thing i dont like is when "Always show all values for inputs" enabled, the search results could be empty "no Results"
as i understand, there is now way of using ajax search with " Show only available options for each input" and the same time using post status filter?

If No, Is it possible to show the View with different Post Stauts i tried following here: https://toolset.com/documentation/beyond-the-basics/re-use-the-same-view-with-different-settings/

but the post status query filter has no option for a shortcode or URL

Then i can show the same view with different status on different pages, instead duplicating the same view and changing the quesry filter of the post status by each of them.
Thanks

#922292

Q1) there is now way of using ajax search with " Show only available options for each input" and the same time using post status filter?
Yes, you are right, as I mentioned above, since the "Post Status:" dropdown menu is not a built-in feature of Views plugin, it does not support option "Show only available options for each input".

Q2) Then i can show the same view with different status on different pages, instead duplicating the same view and changing the quesry filter of the post status by each of them.
The status filter does not support views attribute parameter, see our document:
https://toolset.com/documentation/user-guides/filtering-views-query-by-post-status/
If you agree, we can take it as a feature request, our developers will evaluate it.

Currently, you need to create different view for each post status.

#922360

Yes i think it would be great to add this a feature request!

#922361

Thanks for your great Support Luo!