Skip Navigation

[Resolved] Filtering a view by status with shortcode attribute

This support ticket is created 2 years, 4 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 1 reply, has 2 voices.

Last updated by Minesh 2 years, 4 months ago.

Assisted by: Minesh.

Author
Posts
#2471033

Hi there,

I'm creating a page that shows posts with different statuses on each tab of a tabs element. I would like to create one view and then filter by status using a shortcode attribute, but that option doesn't come up when I set the filter to post status. It only gives me the ability check which statuses i want rather than set them as a shortcode attribute.

Am I missing something here?

#2471273

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

to filter the view with the post status added as view's shortcode attribute, you will have to use the view's filter "wpv_filter_query":

For example, please try to add the following code to "Custom Code" section offered by Toolset:
- https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/

add_filter( 'wpv_filter_query', 'func_filter_shortcode_attr_with_post_status', 99, 3 );
function func_filter_shortcode_attr_with_post_status( $query_args, $view_settings, $views_id  ) {
    if ( $views_id == 99999) { 
      
       global $WP_Views;
       $status = $WP_Views->view_shortcode_attributes[0]['post_status'];
       $query_args['post_status'] = array();
      
       if(isset($status) and $status!=''){
      		$query_args['post_status'][] =  $status; 
       }
      
    }
    return $query_args;
}

Where:
- change the 99999 with your original View ID

Then you can add your view as:

[wpv-view name="your-view-slug" post_status='publish']
[wpv-view name="your-view-slug" post_status='draft']

More info:
- https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query