Skip Navigation

[Resolved] Passing the post type as an argument to [wpv-view] or render_view()

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 3 replies, has 1 voice.

Last updated by Saul Baizman 2 days, 19 hours ago.

Assisted by: Minesh.

Author
Posts
#2793583

Hi there,

This is a strange question. When we create a view, the first decision is to select a content type (post type, taxonomy, or users). I'm typically selecting a post type.

In my case, I have multiple views displaying posts in an identical way but with different post types. Here's my question: is there a way to pass the post type as an argument to a [wpv-view] shortcode or PHP function render_view() via, say, a shortcode parameter or URL parameter?

I know, it's strange, but I'm trying to create more abstract / "reusable" views.

Saul

#2793585

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Can you please try to use the following code:

add_filter('wpv_filter_query', 'func_filter_by_post_type_arg_view_shortcode_attribute', 10, 3);
function func_filter_by_post_type_arg_view_shortcode_attribute($query, $setting,$view_id) {

if($view_id==99999 ){ 
     
    $post_type_arg = "myposttype";
    global $WP_Views;
     
     
    $view_shortcode_attributes = $WP_Views->view_shortcode_attributes;
    if(isset($view_shortcode_attributes[count($view_shortcode_attributes)-1][$post_type_arg])){
        $post_types = $view_shortcode_attributes[count($view_shortcode_attributes)-1][$post_type_arg];
        if (!empty($post_types)) {
            $arr_post_types = array_map('trim', explode(',', $post_types));
            $query['post_type'] = $arr_post_types;
          
             
        }
    }
   }
    return $query;
}

Where:
- Replace 99999 with your original view ID.

And you can call the view as given under:

[wpv-view name='my-view-slug' myposttype='book']

Where:
- Replace 'book' with your original post type.

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

#2793586

Minesh,

This is ridiculously cool! And it will save me a lot of work.

Is it the case that when defining the view in the dashboard, I can just choose *any* post type and it will be replaced via the "myposttype" attribute passed to the shortcode (due to the custom PHP code)?

Saul

#2793677

Minesh
Supporter

Languages: English (English )

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

Yes - exactly.

#2793691

Fantastic! Thanks again for your help!

Saul