WordPress.org

Ready to get started?Download WordPress

Forums

Relevanssi - A Better Search
Bug in admin search for custom post types (20 posts)

  1. manishie
    Member
    Posted 9 months ago #

    Hi,

    I've setup Relevanssi to index my custom post types and custom fields. In the backend when viewing the post type manage screen, if I don't change the default value for the date filter (so it reads "Show All Dates"), when I press the Filter button, no results are shown. From this screen showing no results, if I press the filter button again, then the results are shown.

    This only happens when Relevanssi is turned on. If I turn it off, then the Filter button works as expected. If I don't change the default value and I press the Filter button, all posts are shown.

    I've isolated the problem a bit. After the Filter button is pressed when nothing appears on the screen, the url is:

    http://mysite.com/wp-admin/edit.php?s&post_status=all&post_type=products&action=-1&m=0&paged=1&mode=list&action2=-1.

    The part of the URL which causes the problem is the "?s". There is no value associated with the key "s". With Relevanssi turned on, this returns an empty set. As soon as I turn it off, this same URL works as expected. Also, with Relevanssi on, if I remove that part of the url:

    http://mysite.com/wp-admin/edit.php?post_status=all&post_type=products&action=-1&m=0&paged=1&mode=list&action2=-1

    that also works as expected (showing all posts).

    Thanks for the great plugin!

    mkm

    http://wordpress.org/extend/plugins/relevanssi/

  2. Mikko Saari
    Member
    Plugin Author

    Posted 9 months ago #

    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.

  3. suntower
    Member
    Posted 8 months ago #

    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

  4. Mikko Saari
    Member
    Plugin Author

    Posted 8 months ago #

    No, 's' is the only parameter Relevanssi understands.

  5. suntower
    Member
    Posted 8 months ago #

    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

  6. Mikko Saari
    Member
    Plugin Author

    Posted 8 months ago #

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

  7. Sii Cockerill
    Member
    Posted 8 months ago #

    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?

  8. Sii Cockerill
    Member
    Posted 8 months ago #

    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.

  9. Mikko Saari
    Member
    Plugin Author

    Posted 8 months ago #

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

  10. Mikko Saari
    Member
    Plugin Author

    Posted 8 months ago #

    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?

  11. Sii Cockerill
    Member
    Posted 8 months ago #

    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.

  12. Mikko Saari
    Member
    Plugin Author

    Posted 8 months ago #

    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;
        }
    }
  13. Sii Cockerill
    Member
    Posted 8 months ago #

    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.

  14. Mikko Saari
    Member
    Plugin Author

    Posted 8 months ago #

    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?

  15. Sii Cockerill
    Member
    Posted 8 months ago #

    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.

  16. Mikko Saari
    Member
    Plugin Author

    Posted 8 months ago #

    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?

  17. Sii Cockerill
    Member
    Posted 8 months ago #

    Hi Mikko,

    I replaced the second function with the above. It worked :-)
    I see the problem was the use of $query instead of $wp_query in the if statement.

    Thanks for all your help!

  18. Mikko Saari
    Member
    Plugin Author

    Posted 8 months ago #

    Great, glad to hear it works. I'm posting the solution my knowledge base, in case I need it again.

  19. suntower
    Member
    Posted 8 months ago #

    Sadly, this didn't help me at all. :(

    To refresh: when I type a search term in the search box, I get only Posts, not the Custom Post Type that should be displayed.

  20. Mikko Saari
    Member
    Plugin Author

    Posted 8 months ago #

    Suntower, that's a different problem - I suggest you start a new thread for that.

Reply

You must log in to post.

About this Plugin

About this Topic

Tags