• Resolved philipsimplynuc

    (@philipsimplynuc)


    Hi,

    Love the plugin by the way, light and clean to work with.
    However I have a quick question or issue.

    I’m a full-time WordPress developer of 10 years.
    So I’m very familiar with WordPress and coding result and templated pages for custom loops.

    I have set up the plugin “WPES is disabled for global WordPress search”.
    Also created custom settings for Woocommerce search form.
    I am then adding the hidden field to the website search form.
    Which passes the “wpessid” param when searching for products.
    This is working fine, and the results are correct.

    However, I have also coded a custom widget for the search results page sidebar.
    Here I have a few additional loops to search for the same “search” text in some other post types too.

    However, these additional “new WP_Query()” calls are being affected by the “wpessid” param, and NOT returning posts.

    To demonstrate this issue, if you visit the URL provided above.
    You will see that the sidebar widget has “Pages (1)”
    However, if you remove the “wpessid” from the url and refresh.
    You will see that there are also “News & Blog” post counts also.

    But passing the “wpessid” param is affecting other loops on the same page.
    Even though “WPES is disabled for global WordPress search”.
    I think the issue is because they are on the same page.

    Please note that the 2 additional loops which are not returning results with the “wpessid” param set – News & Blog.
    Are simply querying the standard wordpress posts post_type with “category_name” set to either “blog” or “news”.

    Strange!

    If I query just for “posts” without the “category_name” in these additional loops then I get a result.

    Is there a way to use the Extended Search functionality on if main_query.
    But to NOT use the extended search functionality on other additional loops on the same page. Even with “wpessid” param set.

    I have checked the docs website but the information was very limited.

    Any help would be greatly appreciated.

    Love the plugin

    Kind Regards

    Philip C

    The page I need help with: [log in to see the link]

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Sumit Singh

    (@5um17)

    Hi Philip C,

    Thanks for the feedback!
    About the issue, yes it is expected. WPES design to affect the all search queries either in primary loop or secondary loop on front-end so it can work in plug and play manner.
    And you understand it right since wpessid present in URL it will affect the query.

    Because of this issue I added a query parameter to tell WPES not to change the query when it is present.
    Please check this https://wpes.secretsofgeeks.com/hooks#disable_wpes

    You can set “disable_wpes” to true in your custom WP Query and the issue should be fixed.

    Have a great year ahead!

    Thread Starter philipsimplynuc

    (@philipsimplynuc)

    Hi,

    Thanks for the reply, and your filter works perfect.
    However, I have another small question for you, if you can help please.

    To demonstrate, please visit the following link:
    https://staging.simplynuc.com/?s=Frost+Canyon&post_type=product&wpessid=58050

    On this page we have searched for products of a particular category. With 11 results found.

    In the sidebar you will now see a custom widget. If you click on “Pages” in the sidebar.
    https://staging.simplynuc.com/?s=Frost%20Canyon&post_type=page

    On this page we have now searched for pages with the same search term.
    Now in the sidebar Products only have 7 results found.

    ————–
    The question I have is, on the second page above.
    This custom sidebar widget uses WP_Query to search for products with the same criteria.
    However, I am only getting 7 results.

    In my widgets code, I have 4 WP_QUERY loops for different post types .etc

    I have used your filter code just before the other post types, which sets ‘disable_wpes’ to TRUE. Which has worked fine and uses the GLOBAL search settings from your plugin.

    However, my question is?
    In the last WP_QUERY in my widget, I need to get the same product count 11 as the main woocommerce page itself. And not 7.

    As I mentioned earlier, I am very familiar with WordPress and coding custom queries.
    And I have attempted to set the ‘disable_wpes’ to FALSE. Just before this last WP_QUERY but I am still only getting 7 results.

    Sorry for the long question, but better to fully explain my question.
    I have tracked down the issue to the filters in your plugin.
    ‘wp_es_custom_query’ & ‘wp_es_pre_get_posts’ NOT being respected for this WP_QUERY

    I assumed that by setting ‘disable_wpes’ back to FALSE.
    That the Woocommerce custom search settings I have setup from your plugin will then be applied.

    So: main_query has run using the URL query_vars & your plugin with custom settings.

    Sidebar Widget:
    1. ‘disable_wpes’, TRUE – Meaning the Global settings from plugin for loops below.

    2. WP_Query for PAGES

    3. WP_Query for POSTS – NEWS

    4. WP_Query for POSTS – BLOG

    5. ‘disable_wpes’, FALSE – Meaning the Custom settings from plugin for loops below.

    6. WP_Query for PRODUCTS

    However, I am only getting 7 results / pre_get_posts NOT building the SQL fully.

    Again any help would be greatly appreciated.

    I have tried setting suppress_filters FALSE
    Declaring a new instance of WPES & WPES_Core and manually calling your functions code.

    `
    add_filter( 'pre_get_posts', function ( $query ) {
        $query->query_vars['s']            = $searched;
        $query->query_vars['post_type']    = 'product';
        $query->query_vars['wpessid']      = 58050;
    }, 999);
    
    $args = array (
        's'                     => $searched,
        'fields'                => 'ids',
        'post_type'             => 'product',
        'post_status'           => $status,
        'posts_per_page'        => -1,
    );
    $query = new WP_Query( $args );
    `

    Kind Regards

    Philip C

    Thread Starter philipsimplynuc

    (@philipsimplynuc)

    Sorry just to mention.

    This is just to get the COUNT of products for a search term through your plugin.
    I can code a couple of custom loops and combine the results myself.

    But your plugin is far more efficient using SQL and building using pre_get_posts.

    But how do I ensure WP_QUERY uses your filters.

    I am available for a call via Skype or Zoom if you like to discuss.

    Or if you have any other questions.

    Thanks for your help.

    Great plugin.

    Plugin Author Sumit Singh

    (@5um17)

    Hey,

    It could be because $query->query_vars['wpessid'] = 58050; is still not supported.
    The wpessid only works when present in URL and can not be changed within the query. But this is a great idea to change it from WP query, I will consider adding it. Thank you for mentioning it ??

    For your current situation I will suggest directly setting it to global variable. Like this

    
    if (!empty($_GET['wpessid'])) {
    	$wpessid_from_url = $_GET['wpessid']; //Let's backup the value from URL.
    }
    $_GET['wpessid'] = 58050; //Set our new value just for below query.
    
    $args = array (
        's'                     => $searched,
        'fields'                => 'ids',
        'post_type'             => 'product',
        'post_status'           => $status,
        'posts_per_page'        => -1,
    );
    $query = new WP_Query( $args );
    
    if (isset($wpessid_from_url)) {
    	$_GET['wpessid'] = $wpessid_from_url; //If we changed the value let's restore to original.
    } else {
    	unset($_GET['wpessid']); //Else remove the variable.
    }
    

    Since you are developer you know that it is not good practice to change the $_GET variable directly. But we don’t have other option right now. I will try adding this in future release and then you will be able to pass the wpessid in WP Query.

    Let’s give it a try and I hope it will work.

    Thank you.

    Thread Starter philipsimplynuc

    (@philipsimplynuc)

    Hi,

    I have tried setting the $_GET variable as mentioned above but without any success ??

    Would you believe your suggestion is exactly where my thought process was before I had decided to reach out with you for a second time. Great minds think… ??

    Anyway, thank you very much for your response, I really appreciate it and I now feel really confident in using your plugin long term across our family of websites.

    If you would, please consider adding this feature to the next version of the plugin, I would really appreciate it.
    For the time being, we will just have to accept the count on the secondary widget loops for products is in-correct and not in line with the number of products return by your plugin and its settings.

    It’s a small thing but I’m hoping your next version will fix this for us and I can then amend our widget code to pass ‘58050’ as a $query->query_vars.

    Thanks again

    Great plugin

    Philip C

    Plugin Author Sumit Singh

    (@5um17)

    Hi Philip C,

    Sorry for the waiting.

    I tried to make the said changes for above request and here is what I found.

    $_GET is not working because I am using $_REQUEST, you can see the code here https://github.com/5um17/wp-extended-search/blob/v2.0.3/includes/class-wpes-core.php#L186
    However, setting REQUEST is not working as well because this is done too early in plugin constructor.
    Even I change the code a bit to load the settings again according to WP Query still there are other actions which won’t be executed as init, plugins_loaded. I have to change the whole plugin loading sequence to make it work. I afraid to say it will not be possible at the moment.

    But I found another workaround, which you may try, it will work if you load the secondary loop posts by AJAX or REST API.
    For AJAX you need to include the wpessid in AJAX URL e.g. https://example.com/wp-admin/admin-ajax.php?wpessid=10
    For REST API https://example.com/wp-json/wp/v2/search?search=search-query&wpessid=10

    I would suggest REST API would be very easy as you don’t need to code anything expect the front-end JS.
    One example

    
    fetch('https://localhost/wp-json/wp/v2/search?search=g&wpessid=21')
        .then(data => data.json())
        .then(data => console.log(data.length));
    

    I hope it helps.

    Thanks

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Multiple Loops on Results Page’ is closed to new replies.