Skip Navigation

[Resolved] View Custom Parametric Search for Post Status

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to create a parametric search View that allows users to filter a list of posts by post status, using a dropdown select filter.

Solution: It is not currently possible to create front-end filters for post status. Other workarounds proposed in the past have limitations that are not easily overcome with custom code. Instead, the most practical approach is to create a custom select field that includes values corresponding to each possible post status. Copy the actual post status information into the value of this custom field for each post, and set up a parametric search View that filters upon this custom field.

In this case the posts are created in Forms, and a generic select field is used to allow the User to choose a post status for the created post. There is a custom code snippet using the cred_submit_complete API to programmatically set the post status after the Form is submitted. We can tap into that event hook to also set the custom field value programmatically with update_post_meta:

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']);
        }
    }
}

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_submit_complete
https://developer.wordpress.org/reference/functions/update_post_meta/

This support ticket is created 3 years, 10 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 6 replies, has 2 voices.

Last updated by rithvikU 3 years, 10 months ago.

Assisted by: Christian Cox.

Author
Posts
#1954157
Screenshot 2021-02-17 at 7.33.48 PM.png

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>
#1954739

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.

#1955473
Screenshot 2021-02-18 at 11.43.12 AM.png

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);
        }
    }
}
#1961499

Hi Christian,

It's been a while since I posted my response, can I expect a reply sometime soon?

- Rithvik

#1961805

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.

#1961881

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.

#1962225

Thanks for the help Christian, your solution worked perfectly!