Home > database >  php instruction to add a second engine
php instruction to add a second engine

Time:07-11

I'm having a hard time understanding how to add a second engine in my hook it's this line

$swp_query = new \SWP_Query( array(
        'engine'   => 'default'
        // engine 2 here I need to add Supplemental Engine
        's'        => $wp_query->query['s'],
        'fields'   => 'ids',
        'nopaging' => true
    ) );

full code here

<?php

// Integrate SearchWP with JetSmartFilters search using
// JetEngine Listing Grid to display results.
// @link https://searchwp.com/documentation/knowledge-base/compatibility-with-jetsmartfilters-for-elementor/
add_action( 'pre_get_posts', function( $wp_query ) {
    if (
        ! isset( $wp_query->query['jet_smart_filters' ] )
        || empty( $wp_query->query['s'] )
    ) {
        return;
    }

    $swp_query = new \SWP_Query( array(
        'engine'   => 'default',
        's'        => $wp_query->query['s'],
        'fields'   => 'ids',
        'nopaging' => true
    ) );

    $results = ! empty( $swp_query->posts ) ? $swp_query->posts : array( 0 );

    $wp_query->set( 'post__in', $results );
    $wp_query->set( 'post_type', 'any' );
    $wp_query->set( 'post_status', 'any' );
    $wp_query->set( 'orderby', 'post__in' );
    $wp_query->set( 'order', 'DESC' );
    $wp_query->set( 's', false );
}, 9999 );

I tried both methods below but it doesn't seem to work that way.

test 1

$swp_query = new \SWP_Query( array(
        'engine'   => 'default','supplemental',
        's'        => $wp_query->query['s'],
        'fields'   => 'ids',
        'nopaging' => true
    ) );

test 2

$swp_query = new \SWP_Query( array(
        'engine'   => 'default',
        'engine'   => 'supplemental',
        's'        => $wp_query->query['s'],
        'fields'   => 'ids',
        'nopaging' => true
    ) );

what did I not understand?

source 1 source 2

CodePudding user response:

Each item in a PHP array is identified by its unique key. The code you are calling is probably doing something like $parameters['engine'] which will always give it the single item identified by the key 'engine'.

There is no way to have two items with the same key. If you write the same key twice, the second value will just overwrite the first.

The closest you can get is to have one item that is itself an array, e.g. $example = ['list_of_engines' => ['default', 'supplemental']] will mean that $example['list_of_engines'] is the array ['default', 'supplemental'].

It's really important to note that this won't work unless the code you're calling is expecting it. If it expects $parameters['engine'] to be a string, passing an array there will just give an error. So you'll need to check the documentation to see what format it expects.

CodePudding user response:

I suspect you've to instantiate the engines just twice with different variable names:

    $searchwp_query_1 = new \SearchWP\Query( $search_query, [
        'engine' => 'default', // The Engine name.
        'fields' => 'all',          // Load proper native objects of each result.
        'page'   => $search_page,
    ] );

    $searchwp_query_2 = new \SearchWP\Query( $search_query, [
        'engine' => 'supplemental', // The Engine name.
        'fields' => 'all',          // Load proper native objects of each result.
        'page'   => $search_page,
    ] );
  • Related