Skip Navigation

[Resolved] Modify View Post Type/s by / via Shortcode

This support ticket is created 3 years, 7 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 6 replies, has 2 voices.

Last updated by michaelA-13 3 years, 7 months ago.

Assisted by: Shane.

Author
Posts
#2031081

Tell us what you are trying to do?

Hello, I found a helpful thread that does what I need for 90% of the cases. But I would need to be able to set more than one post type occasionally .

Is there any documentation that you are following? - Is there a similar example that we can see?

https://toolset.com/forums/topic/modify-filter-by-post-type-shortcode-function-for-use-with-multiple-views/

i.e. [wpv-view name="countries-table" type="countries"] works fine

e.g. [wpv-view name="countries-table" type="communities,countries"] does not

THX

#2031161

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Michael,

Thank you getting in touch.

To do this you will need to convert the comma separated string to an Array.

So it should be modified to

add_filter( 'wpv_filter_query', 'fbpts', 1000 , 3 );
function fbpts( $query_args, $view_settings ) {
   
    $view_ids_array = array(1111, 2222, 3333); 
       
    if ( !is_admin() && isset($view_settings['view_id']) && in_array($view_settings['view_id'], $view_ids_array) ) {   
       
    global $WP_Views;
    $attributes = $WP_Views->view_shortcode_attributes;
    $att_array = explode(',', $attributes[0]['type']);
    if (!empty($att_array)) {
    $query_args['post_type'][0] = $att_array;
            }
        }
    return $query_args;
}

Please try this and let me know if it helps.
Thanks,
Shane

#2037441

Hello!

Sorry for delayed reply. Regrettably your code does not work :/

It seems to always returns "posts" in the view rather than the set post types.

E.g. with [wpv-view name="countries-table" type="communities,countries"] the view displays posts as type.

THX

#2038251

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Michael,

Thank you for getting in touch.

Would you mind allowing me to have admin access to the site so that I can debug to see what is wrong here?

I've enabled the private fields for your next response. Also please let me know the page that you are testing this out on.

Thanks,
Shane

#2040641

I removed your code from the functions file for now.

This post has the complete shortcode hidden link

Michael

#2041169

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Michael,

Thank you for the credentials, I was able to fix the code. The below is the correct and final function.

add_filter( 'wpv_filter_query', 'fbpts', 99 , 3 );
function fbpts( $query_args, $view_settings, $view_id ) {
    
    $view_ids_array = array(3762); 
        
    if ( !is_admin() && isset($view_settings['view_id']) && in_array($view_id, $view_ids_array) ) {   
        
    global $WP_Views;
    $attributes = $WP_Views->view_shortcode_attributes;
    $att_array = explode(',', $attributes[0]['type']);
    if (!empty($att_array)) {
    $query_args['post_type'] = $att_array;
            }
        }
    return $query_args;
}

I've already added and activated this code in Toolset->Settings -> Custom Code.

Please let me know if you're seeing the correct results on your end now.

Thanks,
Shane

#2041333

My issue is resolved now. Thank you!