I am following this (https://toolset.com/forums/topic/parametric-search-to-filter-a-view-by-post-status/) ticket to create a custom search filter to search posts in the front end based on post status. I used the shortcodes and custom php mentioned by Minesh there and it works for the most part.
Problem:
The first time I click the submit button the correct parameters are passed to the url. However, when I select a different post status and click submit again, new parameters are passed to the url but the old parameters are not removed. Same issue when I click the reset button too. It resets the url parameters for all filters except the custom search filter for the post status.
This is the URL after changing the filters a few times and clicking reset. You can see how the parameters are repeated:
hidden link
Here is the link to the page where the issue appears:
hidden link
Access is restricted to logged-in users, I can provide the log in details if needed.
PHP Code added in functions.php of child theme:
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 == 3484 ) {
if(isset($_GET['wpv-post-status'])){
$query_args['post_status'] = $_GET['wpv-post-status'];
}
}
return $query_args;
}
HTML and shortcodes used for custom filter:
<div class="wp-block-toolset-views-custom-search-filter wpv-custom-search-filter wpv-custom-search-filter-label-before alignleft">
<div class="form-group">
<label class="wpv-custom-search-filter__label" for="post-status" style="font-size:20px;">Status</label>
<!-- ## -->
<span class="wpv-custom-search-filter__input">[wpv-control field="wpv-post-status" url_param="wpv-post-status" type="select" values="any,publish,pending,draft" display_values="All,Published,Under Review,Draft"]</span>
</div>
</div>
Hello, parametric search using post status isn't well supported with the custom code mentioned in that older ticket. The most effective way to filter by post status is to create a custom "select" field that includes the different available post statuses, and to create a front-end filter based on that custom field. Otherwise, the URL format becomes unmanageable, as you have already noticed.
If the posts are created by Toolset Forms on the front-end of the site, I can show you how to use a custom code snippet to set the custom field value automatically when posts are created. I would need to know the ID of the Form used to create posts, and the slug of the field used to store the post status.
Hi Christian,
That would be helpful, thank you very much. The form IDs are 737 and 521, and the slug of the custom select field I just created to store the post status is 'post-status' (see screenshot for option values). Just one complication, I already use a generic field in the form along with a code snippet to alter the post status. I think it has to be a generic field because I offer different post status options in the form based on the access level of the user. So I would assume that the php code you are going to provide will need to be executed after the code snippet related to the generic field. Let me know if you can think of a better way to go about this or if I am mistaken about something.
Generic Field as seen by an author:
[cred_generic_field type='radio' field='post_stat']
{
"required":1,
"default":["publish"],
"persist":1,
"options":[{"value":"publish","label":"Publish"},{"value":"draft","label":"Draft"}]
}
[/cred_generic_field]
PHP:
// Allow Saving of Drafts
add_action('cred_submit_complete', 'my_save_draft',10,2);
function my_save_draft($post_id, $form_data)
{
// if a specific form
if ($form_data['id'] = array(737,521))
{
if (isset($_POST['post_stat']))
{
// add it to saved post meta
$my_post = array(
'ID' => $post_id,
'post_status' => $_POST['post_stat'],
);
wp_update_post($my_post);
}
}
}
Hi Christian,
It's been a while since I posted my response, can I expect a reply sometime soon?
- Rithvik
Hi, sorry for the delay. I do not work on Fridays and Saturdays and this is the first chance I've had to review your response. I'll run some tests on a local environment and give you an update shortly.
Okay I'll show you how to update your code to also set the custom field value programmatically. You'll use the WordPress function update_post_meta to update the custom field value like so:
add_action('cred_submit_complete', 'my_save_draft',10,2);
function my_save_draft($post_id, $form_data)
{
// if a specific form
if ($form_data['id'] = array(737,521))
{
if (isset($_POST['post_stat']))
{
// update post status
$my_post = array(
'ID' => $post_id,
'post_status' => $_POST['post_stat'],
);
wp_update_post($my_post);
// add / update post-status custom field value
update_post_meta( $post_id, 'wpcf-post-status', $_POST['post_stat']);
}
}
}
Note that the 'wpcf-' prefix is required for the custom field slug in the update_post_meta call. This should set the custom field value based on the chosen status in the generic field. Documentation for this function is available here:
https://developer.wordpress.org/reference/functions/update_post_meta/
Let me know if you have additional questions about this update and we can discuss in more detail.
Thanks for the help Christian, your solution worked perfectly!