Support

Account

Home Forums General Issues Post Object Query Filter Issue

Helping

Post Object Query Filter Issue

  • Hi guys. I have the following function that I am hooking into a post object field (query) To filter posts user specifically:

    
    add_filter('acf/fields/post_object/query/name=which_event_is_this_document_for', 'my_post_object_query_for_specific_user', 10, 3);
    
    function my_post_object_query_for_specific_user( $args, $field, $post )
    {
    
        global $current_user, $wp_roles; get_currentuserinfo();
    
        $user_id = $current_user->ID;
    
        $events = get_field('events', 'user_'.$user_id ) ? get_field('events', 'user_'.$user_id ) : false;
    
            $event_info = array();
    
            if($events){
    
                foreach( $events as $event){
    
                    $event_info[] = $event->ID;
                    
                }
    
                $args['include'] = $event_info;
    
            }
              
            $args['orderby'] = 'meta_value';
    
            $args['meta_key'] = 'date';
    
        return $args;
    }

    This works absolutely fine. However, If I introduce a something into the query argument that causes no results;

    for example

    $args['post_parent'] = 9999999; //Testing Purposes

    I get a ‘Loading Failed’ error. I had a look at the select2.js code and it looks like it should be returning ‘No matches’. (Bug?)

    I would ideally like to hook into this message and display ‘You have no events’.

    Is there a way to do this?

  • Hi @magicstick,

    Thanks for the post.

    I would recommend you make use of the acf/validate_field filter to attach some custom validation to the field and return a custom error message.

    I hope this helps.

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Post Object Query Filter Issue’ is closed to new replies.