Advanced search: category selection

This topic contains 10 replies, has 2 voices, and was last updated by  Stiofan O’Connor 5 years, 8 months ago.

We have moved to a support ticketing system and our forums are now closed.

Open Support Ticket
  • Author
    Posts
  • #445403

    Silvia Frati
    Expired Member
    Post count: 30

    Hello,

    We have purchased all the extension bundle, installed and enabled the advanced search and enabled category search, until here, no problem at all.

    WP version: 4.9.6
    GD: 1.6.27
    GD advanced search filter: 1.4.93
    GD CPT: 1.3.7

    We have a doubt about the query performed when searching for one specific category, using the standard GD home page search we enter no text in the text search and select one category.
    The $_GET looks fine:

    
    
    
    s = ""
    sgd_placecategory = array("20") 
    

    but when we put a breakpoint in the wpquery->get_post we find out that the query executed ignores the selected category and instead search for ALL the avaible categories, this is what it looks like:

    
    
    
    [...]
    OR FIND_IN_SET(16, wp_geodir_gd_place_detail.gd_placecategory) OR FIND_IN_SET(19, wp_geodir_gd_place_detail.gd_placecategory) OR
       FIND_IN_SET(20, wp_geodir_gd_place_detail.gd_placecategory) OR FIND_IN_SET(21, wp_geodir_gd_place_detail.gd_placecategory) OR
       FIND_IN_SET(22, wp_geodir_gd_place_detail.gd_placecategory) OR FIND_IN_SET(23, wp_geodir_gd_place_detail.gd_placecategory) OR
    [..]
    

    And so on with all the ids of our categories.
    Digging into the code it looks like this series of FIND_IN_SET comes from file: wp-content/plugins/geodirectory/geodirectory-functions/listing_filters.php:840~ inside the function searching_filter_where

    After that the file /wp-content/plugins/geodir_advance_search_filters/geodirectory_advance_search_function.php:423 gets called,function geodir_advance_search_where and this adds correctly in the query just the FIND_IN_SET for the selected category id present in the $_GET var, but obviously the query is now compromised, since it actually create a duplicate of that filter because it’s present already when passing in the first file.

    The final query looks something like this:

    
    
    
    SELECT SQL_CALC_FOUND_ROWS  wp_posts.*, wp_geodir_gd_place_detail.*  FROM wp_posts  LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) INNER JOIN wp_geodir_gd_place_detail ON (wp_geodir_gd_place_detail.post_id = wp_posts.ID) 
     WHERE 1=1  AND (	( wp_posts.post_title LIKE "" )
                                  
    							 <strong>OR FIND_IN_SET(16 , wp_geodir_gd_place_detail.gd_placecategory)  OR FIND_IN_SET(19 , wp_geodir_gd_place_detail.gd_placecategory)  OR FIND_IN_SET(20 , wp_geodir_gd_place_detail.gd_placecategory)  OR FIND_IN_SET(21 , wp_geodir_gd_place_detail.gd_placecategory)  OR FIND_IN_SET(22 , wp_geodir_gd_place_detail.gd_placecategory)  OR FIND_IN_SET(23 , wp_geodir_gd_place_detail.gd_placecategory)  OR FIND_IN_SET(25 , wp_geodir_gd_place_detail.gd_placecategory)  OR FIND_IN_SET(26 , wp_geodir_gd_place_detail.gd_placecategory)  OR FIND_IN_SET(27 , wp_geodir_gd_place_detail.gd_placecategory)</strong>  OR FIND_IN_SET('tag1' , wp_geodir_gd_place_detail.post_tags)  OR FIND_IN_SET('tag2' , wp_geodir_gd_place_detail.post_tags)  OR FIND_IN_SET('tag3' , wp_geodir_gd_place_detail.post_tags)  OR FIND_IN_SET('tag4' , wp_geodir_gd_place_detail.post_tags)  
    					    ) 
    					
    				AND wp_posts.post_type in ('gd_place')
    				AND (wp_posts.post_status = 'publish')  <strong>AND ( FIND_IN_SET('20', wp_geodir_gd_place_detail.gd_placecategory )</strong> ) GROUP BY wp_posts.ID ORDER BY  wp_geodir_gd_place_detail.is_featured asc, wp_posts.post_date desc, wp_posts.post_title  LIMIT 0, 10
    

    Are we doing something wrong?

    #445627

    Stiofan O’Connor
    Site Admin
    Post count: 22956

    Hello,

    I am going to have a look at this now and see whats going on.

    Thanks,

    Stiofan

    #445628

    Stiofan O’Connor
    Site Admin
    Post count: 22956

    Hi Silvia,

    Thanks for reporting this, there is a bracket in the wrong place, i have fixed it and it will be in the next release.

    If you want the patch now you can simply replace this file in the plugin: https://github.com/GeoDirectory/geodirectory/blob/master/geodirectory-functions/listing_filters.php

    Thanks,

    Stiofan

    #445634

    Silvia Frati
    Expired Member
    Post count: 30

    Hello Stiofan,

    Thank you for the fix, it does indeed fix the category search issue and actually solves another problem we’ve found digging that code ( if the search text was empty it would force a search WHERE post_title LIKE “” which actually may break the result set).

    Sorry to be a pain but there’s something else we’ve found, with the code you provided (and even the old one) line 783~ of the code in wp-content/plugins/geodirectory/geodirectory-functions/listing_filters.php states:

    
    
    
    function searching_filter_where( $where ) {
        [...]
        $where               = '';
        [...]
    }
    

    This actually negates all the possibile other where that any 3rd party plugin might inject in the query. I don’t really know if this was the desired results but it’s actually a little annoying. Ofc we could inject stuff with higher priority then your code but it’s not always a practical solution.

    Could you fix that too someway? I noticed that doing so you actually clean some “garbage” which comes from the default search, but on the other hand you may destroy something an user might actually want to keep

    #445650

    Stiofan O’Connor
    Site Admin
    Post count: 22956

    That is done very much on purpose, its set at priority 1, the default is 10 so we usually avoid blanking anything important, its crazy the things that mess up the query, even things like a coming soon page plugin. You should have no problems with it if you use anything above priority 1 🙂

    Thanks,

    Stiofan

    #445740

    Silvia Frati
    Expired Member
    Post count: 30

    Let me explain our case which we may be doing it wrong:

    In the project we’re working on with GD we need to add a special tax query part during the GD search query. Now what we have is this:

    add_action('pre_get_posts', 'geodir_listing_loop_filter', 1);

    which later adds:

    add_filter( 'posts_where', 'searching_filter_where', 1 );

    which as we said clears the $where part of the query.

    Now, since the part we have to add is actually a tax query part which get parsed inside WP_Query->parse_query line ~840 we added an hook to pre_get_posts in order to feed the query with the needed tax query part (i could do it in other ways too but still it would happen before the next things i guess).

    Now going down the code the ->parse_query method starts firing the ‘posts_where’ filters, the bad starts here since WP actually has inside the $where the already parsed tax_query part, which the code inside ‘searching_filter_where’ actually clean and delete 🙁

    Is this a bad case we just caused ourself?

    #445767

    Stiofan O’Connor
    Site Admin
    Post count: 22956

    I think the best thing here is if you tell me what you are trying to do exactly and i’ll tell you the best way to achieve it 🙂

    Stiofan

    #445770

    Silvia Frati
    Expired Member
    Post count: 30

    Sure why not, we’re still thinking what’s the best way to achieve this using GD but what we need is to do:

    Customer request:
    Make categories linked to tags, so that picking up a category would search the linked tags. This should be valid in add listing + search

    Consideration:
    We are aware that WP doesn’t make categories and cat linked by default so we need a way to do that. We opted for making a custom tag field in add listing + search because your existing post_tag field doesn’t suite our needs

    Possible solution we came up with:
    1) Build a custom DB table that link cats and tags (2 cols table, id_cat,id_tag) and create the desired link. We manage the data in there by a custom wp_admin page, no issue with that, pretty straightforward
    2) Hide the default GD tags field in add listing and sostitute it with a custom made which is a select that get populated in AJAX reading from point 1) table. We can’t let the user picks random tags (which then fill the db with unwanted data)
    3) Make sure that when saving a new listing all the data gets correctly filled (your field in wp_geodir_gd_place_detail.post_tags + taxonomy association)
    4) Add a custom field (by code) to your search widget which has the same behaviour as the select in the add listing page (so filled in ajax when selecting a category)

    From now on we get to the point stated above, picking up a category + [x] number of tags would perform a query on gd_place_tags taxonomy. This is where we need to feed the global $wp_query with our custom tax query reading the custom field

    Hope it’s clear 🙂

    SIDENOTE: testing this out we noticed an issue with tags association in backend compared to the data in the DB, do you prefer us to open a new forum post about this issue?

    #445781

    Stiofan O’Connor
    Site Admin
    Post count: 22956

    OK thanks for the explanation i think i understand what you are doing now.

    We have a task in place to add a search param for tags which might help you for the search side of things and mean you don’t need to add ur filter.

    Are you at that stage now, or have a deadline?

    Stiofan

    #445791

    Silvia Frati
    Expired Member
    Post count: 30

    It’s a kinda weird customer, let’s say we are still in stage but we fear a deadline to appear all of a sudden. Anyways going on with what we told u in the prev post we keep developing.

    I guess we actually managed to achieve what we need and inject the query parts we need. You actually answered our question as well so if you want to mark this as closed feel free.

    Thank you for your patience & time

    #445798

    Stiofan O’Connor
    Site Admin
    Post count: 22956

    No problem, i was going to help imple,emt our part sooner if you had a deadline but if you have your filter working then i guess its all good 🙂

    Stiofan

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

We have moved to a support ticketing system and our forums are now closed.

Open Support Ticket