Skip Navigation

[Gelöst] using „post__in“ with the $query_args for „wpv_filter_query“ filter hook

Dieser Thread wurde gelöst. Hier ist eine Beschreibung des Problems und der Lösung.

Problem:
Client is using the wpv_filter_query hook to modify the Query Arguments before a View query is run, but it doesn't seem to be having any effect.

Solution:
The wpv_filter_query filter should be used with a priority of at least 99, or any changes made may be overwritten.

Relevant Documentation:
https://wp-types.com/documentation/programmer-reference/views-filters/#wpv_filter_query

This support ticket is created vor 5 Jahre, 7 Monate. 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+01:00)

This topic contains 2 Antworten, has 2 Stimmen.

Last updated by mohammadD vor 5 Jahre, 7 Monate.

Assisted by: Nigel.

Author
Artikel
#1106488
Screen Shot 2018-09-12 at 12.29.25 PM.png

hello

i have two post_types: 'course' and 'course-group' which course-groups are the children of courses
in a view of 'course-groups' i want to filter them by the post_title of parent 'course'

i'm doing this by a php code:

add_filter( 'wpv_filter_query', 'wpv_filter_query_callback' );
function wpv_filter_query_callback( $query_args ) {

    $search_filters = false;

    if( $query_args['post_type'][0] == 'course-group' ){

        if( isset($_GET['course-name-for-course-group'])){

            if(!isset($query_args['post__in']))
                $query_args['post__in'] = array();

            $search_filters = true;

            $args = array(
               'post_type' => 'u_course',
               's'  => sanitize_text_field($_GET['course-name-for-course-group']),
                'fields'    => 'ids'
            );

            $posts = get_posts( $args );

            foreach ($posts as $item){
                $course_groups = toolset_get_related_posts(
                    $item, // get posts related to this one
                    'u_course-course-group', // relationship between the posts
                    'parent', // get posts where $writer is the parent in given relationship
                    100, 0, // pagination
                    array( ), // How was his surname, again…?
                    'post_id'
                );

                log_status($item);

                log_status(serialize($course_groups));

                foreach ($course_groups as $value)
                    if(!in_array($value , $query_args['post__in']))
                        array_push($query_args['post__in'] , (int)$value);


            }

        }

    }

    log_status(serialize($query_args));
    return $query_args;


}

the "log_status" function, logs sth in a file "log.txt"
and the screenshot is the result of $query_args before returning it

the ids are correct but the view shows all of the course-groups

#1106712

Nigel
Supporter

Languages: Englisch (English ) Spanisch (Español )

Timezone: Europe/London (GMT+01:00)

Without look too closely at your code, I think the issue is the priority with which you are using the wpv_filter_query filter.

By not specifying a priority you are defaulting to 10.

Officially it needs to be 99 to avoid the possibility of any of your changes to $view_args being overwritten, and in practice I always use 101, e.g. here is my boilerplate:

function tsupp_custom_query( $view_args, $view_settings, $view_id ){

    if ( in_array( $view_id, array( 66, 77 ) ) ) { // Edit for View IDs

        // do stuff
    }
    return $view_args;
}
add_filter( 'wpv_filter_query', 'tssupp_custom_query', 101, 3 );
#1107504

the 99 priority works well! but with any other priority it even doesn't pass $view_id and $view_settings into the callback function...

anyway, my problem solved
Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.