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.
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
I have modified the code. seems not to work!
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
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
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];
}
...
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
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
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
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"
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"]
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
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.
Yes i think it would be great to add this a feature request!
Thanks for your great Support Luo!