Home Forums Bug Reports Page Link and Post Object Field Select2 Search Not Working


Page Link and Post Object Field Select2 Search Not Working

  • WP Version: 4.3.1
    ACF PRO Version:

    The search/autocomplete functionality that Page Link and Post Object fields employ via the Select2 JavaScript plugin no longer seem to be working properly. There are two distinct issues I’ve noticed so far:

    1. Searching for keywords will only search through the default 20 preloaded results. Even though I know a keyword would exist in a post title if I were to scroll down to trigger the AJAX loading of new results, it won’t find it. It just highlights the first post in the list as blue.

    2. When more than one post type is listed (post, pages, other custom post types), the other non-matching posts don’t get hidden as you start typing. If you eventually scroll down to the post you’re looking for, it will have the underline that indicates it was found by the search, but it was only moved up to the top of its respective group, it’s not moved up to the top of the entire search field, so it’s unusable.

    For example, the Page Link field below lists Posts and Artists (custom post type). In the first screenshot, you can see that I have manually scrolled down to an artist called Datsik. It is in the result set.

    Screenshot 1

    But, in the second screenshot, when I actually go to type in Datsik, it just highlights the first post and doesn’t find the artist.

    Screenshot 2

    This is hard to describe, so please let me know if any clarification is needed.

  • Hi @jackrugile

    Just to make sure:

    1. Do you get any JS errors if you check your console?
    2. Do you have any other plugins that uses select2? If so could you try disabling them?

  • Hey Jonathan,

    Thanks for your reply. I just took a look and I’m getting no JS errors in the console. Also, I verified that no other plugins are using Select2.

    That triggered some other thinking though. I decided to try out the Relationship field, which has a similar search feature that does not use Select2. I am running into similar issues with that as well.

    Is it possible that the Page Link, Post Object, and Relationship field all utilize the same method or similar methods that could have been changed within the last few updates? I know they were working fine a few versions ago. I’m not sure at what point it started to break.

    Any thoughts on that?

  • Hi,

    Thanks for checking!

    I believe they all use either the same or similar AJAX lookups yes.
    Could you also try the user field? If you have more than 5 users on your site 🙂

    I used the user field myself yesterday on a site with 4 000 users and while it wasn’t very fast (not strange with that amount) it did work.

  • Will do!

    I can setup a quick test installation after the holiday weekend on Monday and give it a try. Thanks again for your help. Will report back here soon.

  • Ok, just did a test on a local installation with a large batch of dummy user accounts. The search functionality did work for the user field.

    However, I noticed that field behaves differently than the others. It preloads all of the users into the select box, unlike the other fields, which only load 20 at a time. Then, more are loaded dynamically as you scroll.

    That was confirmed after comparing the source of the user field with the others.

    So, I could modify the other fields to load all data, but I’d prefer not to. I’d prefer to have it search/load everything only when I actually type in the search box.

    Any thoughts on that?

  • I think I see the issue.

    One line 1558 of api/api-helpers.php it is sorting the results using

    'orderby' => 'menu_order title',
    'order'   => 'ASC',

    For some reason, even though posts_per_page is set to 20, this was not respecting the ‘s’ query arg and returning 20 posts ordered alphabetically. The solution I found was to add a filter on ‘acf/fields/post_object/query’ and set the ‘orderby’ arg to null. This will override the default ‘menu_order title’ argument, and worked for me.

    function acf_post_object_custom_query( $args, $field, $post_id ) 
        $args['orderby'] = null;
        return $args;
    add_filter( 'acf/fields/post_object/query', 'acf_post_object_custom_query', 10, 3 );

    These were my results searching for ‘traffic’.
    Searching for traffic before filter

    Searching for traffic after filter

    If there is a better solution, please post it.

  • Hey aaronjheinen, thanks for posting your solution. I gave it a try, but the behavior doesn’t seem to be fixed for me. I might be placing the filter in the wrong location though. I added it to my functions.php file. Should I be adding that code somewhere else?

  • functions.php is where you would add it.

    This solution is for a Post Object type specifically. If your field is a page_link you’d want to change the filter to

    add_filter( 'acf/fields/page_link/query', 'acf_post_object_custom_query', 10, 3 );

  • Oh ok, yeah, I was using a page link instead of a post object. However, it still doesn’t seem to be fixing the issue.

    One question about your setup: with your custom field, are you using any of the “Filter by Post Type” types? Or are you just leaving that field blank? That’s the only difference I can think of between are setups.

  • Yep, I’m filtering on a specific post_type, but only 1, not multiple post_types.

  • You could try adding debug information into the method

    function acf_get_grouped_posts( $args ) {

    beginning on line 1547 in api/api-helpers.php

    The issue for me was the

    $posts = get_posts( $args );

    call on line 1583 was not using the “s” arg correctly. I debugged it by dumping out the $args var and trial / error.

  • Ok, I can confirm that your fix works when only using a single post type filter. However, it doesn’t fix the issue for me when multiple are selected (or when nothing is selected, which is essentially “All”).

    Is this something you could test and confirm on your end?

  • That’s really strange. I added multiple post_types and changed to ‘all’ to test and it seems to be querying / grouping correctly.

    Multiple post types

  • Shoot, can’t replicate that fix :/ The list just gets locked on the first post of the “Posts” post type and won’t drill down any deeper. It doesn’t even hide the posts in the “Posts” post type that don’t match the query. Just like in my original screenshot.

    Ones last question, how many total News posts and Event posts do you have? Are there more than 20, or less than 20?

    I’ll look at this a little closer later on. There must be some difference in the way I have things setup that’s causing this issue. I appreciate your help on this!

  • Over 2000 total News posts which is how I found this issue in the first place b/c I needed to be able to search on them rather than scroll through =P

  • Ok, finally found the reason for this issue. I was using some script that conflicted and modified the search string. It was essentially searching for a blank string each time. The script I was using was to fix some search issue with the media library page here. Anyway, the accepted answer here WILL conflict with this ACF field.

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

The topic ‘Page Link and Post Object Field Select2 Search Not Working’ is closed to new replies.