WordPress.org

Support

Support » Plugins and Hacks » Bug in admin search for custom post types

Bug in admin search for custom post types

Viewing 15 replies - 1 through 15 (of 19 total)
  • Plugin Author Mikko Saari
    Participant

    @msaari

    I’ve seen this happen before, and I’m not quite sure where that “s” comes from. I got this sorted out on one site, but I don’t remember how I did it… might’ve been some kind of $wp_query hack that removes the “s” parameter, if it’s empty, or something like that.

    suntower
    Member

    @suntower

    Hi Mikko,

    I’m having the same issue.

    If I want to perform a search of a custom post type from the admin, the ?s=my_query returns an empty set.

    I can live with this, but is there another parameter I can substitute which -will- work? In other words, is there a URL parameter I can type in by hand which will send the proper query to Relevanssi?

    TIA,

    —JC

    Plugin Author Mikko Saari
    Participant

    @msaari

    No, ‘s’ is the only parameter Relevanssi understands.

    suntower
    Member

    @suntower

    Thanks, but then I’m a bit confused.

    From the WP Admin the URL might be:

    http://mysite.com/wp-admin/edit.php?post_type=docs

    If I try to search for ‘information’ in a ‘docs’ post type, the URL might be:

    http://mysite.com/wp-admin/edit.php?s=information&post_status=all&post_type=docs&action=-1

    And this will either list an empty set -or- all posts with the search term ‘information’ regardless of the post_type.

    However, if I remove the post_type parameter, the search works as expected (just searches for posts or pages)

    Is there no way for me to manually type in a search URL in the WP-Admin that Relevannsi understands for custom post types?

    TIA,

    —JC

    Plugin Author Mikko Saari
    Participant

    @msaari

    Suntower, hard to say – on my site post_type works as expected in admin. You can try using post_types instead of post_type.

    Sii Cockerill
    Member

    @designtwelve

    Hi Mikko,

    I’m experiencing the exact same issue. Enabling relevanssi for admin search seems to pass an empty ‘s’ query value which stops all results being returned. Another press on ‘Filter’ and the ‘s’ is removed from the query string and all the results return as expected.

    Any idea what the fix was you added to remove the s from the query string?

    Sii Cockerill
    Member

    @designtwelve

    From my custom post types list, the first time I click Filter it seems to run the Relevanssi search with an empty query.

    When I click Filter from that screen, Relevanssi does not run and results appear.

    print_r( $wp_query ); on line 37 of the plugin search.php and click Filter over and over.

    I don’t think Relevanssi should kick in when I click Filter – only when I click Search. If that’s not possible, all empty searches should be ignored.

    Plugin Author Mikko Saari
    Participant

    @msaari

    I’ll take a look at this and see if it can be fixed for the next version.

    Plugin Author Mikko Saari
    Participant

    @msaari

    I’m trying to reproduce the error, but I can’t, I’m seeing the posts every time, no matter what. is_search() is returning false, as it should.

    Hm, that’s tricky. I can’t just block Relevanssi from running if there’s no search term, because some people want to run Relevanssi like that.

    Those of you who have this problem, could you please try adding this code to lib/search.php:

    if ($wp_query->is_admin && empty($wp_query->query_vars['s'])) {
    		$search_ok = false;
    	}

    Put it in relevanssi_query(), before this line:

    $search_ok = apply_filters('relevanssi_search_ok', $search_ok);

    Does that help?

    Sii Cockerill
    Member

    @designtwelve

    Hi Mikko,

    I tried what you suggest. The behaviour was not affected, so I echo’d a string within the if statement – the output was always present.

    I am using the wp-types plugin to create my CPT.

    Plugin Author Mikko Saari
    Participant

    @msaari

    Ah, I managed to find the solution I used before. Does adding this code to your functions.php help?

    add_filter('relevanssi_admin_search_ok', 'relevanssi_disable_filter_admin', 10, 2);
    function relevanssi_disable_filter_admin($ok, $query) {
        if (empty($query->query_vars['s']) || $query->query_vars['s'] == " ") {
            return false;
        }
        else {
            return $ok;
        }
    }
    
    add_filter('relevanssi_search_ok', 'relevanssi_disable_filter');
    function relevanssi_disable_filter($ok) {
        global $wp_query;
        if (empty($query->query_vars['s']) || $query->query_vars['s'] == " ") {
            return false;
        }
        else {
            return $ok;
        }
    }
    Sii Cockerill
    Member

    @designtwelve

    Hi Mikko,

    Yes and no.

    Adding those filters to functions.php stops the alternating filter issue
    *BUT* it completely disables the CPT search function in the process.

    Plugin Author Mikko Saari
    Participant

    @msaari

    Ah, drop the second one. I’m not sure where that came from, but it can’t work. Just keep the first one. Does that help?

    Sii Cockerill
    Member

    @designtwelve

    Removing the second filter re-enables the search 🙂

    But the first filter on its own does not solve the filter bug 🙁

    add_filter('relevanssi_admin_search_ok', 'relevanssi_disable_filter_admin', 10, 2);
    	function relevanssi_disable_filter_admin($ok, $query) {
    	    if (empty($query->query_vars['s']) || $query->query_vars['s'] == " ") {
    	    	echo 'no';
    	        return false;
    	    }
    	    else {
    	    	echo 'yes';
    	        return $ok;
    	    }
    	}

    Testing this example, ‘no’ is output the first time I click Filter (and there are no results), then there is no echo’d output but results are shown on the second click.

    Plugin Author Mikko Saari
    Participant

    @msaari

    Modify the second function to:

    add_filter('relevanssi_search_ok', 'relevanssi_disable_filter');
    function relevanssi_disable_filter($ok) {
        global $wp_query;
        if (empty($wp_query->query_vars['s']) || $wp_query->query_vars['s'] == " ") {
            return false;
        }
        else {
            return $ok;
        }
    }

    Does that help? If not, if you echo the value of $search_ok just before relevanssi_do_query() is called, is it true or false?

Viewing 15 replies - 1 through 15 (of 19 total)
  • The topic ‘Bug in admin search for custom post types’ is closed to new replies.