• I’m using pre_get_posts to add a bunch of options from a custom search form to the $query’s tax_query via $query->set( 'tax_query', ...) which Relevanssi then gets a hold of it during relevanssi_do_query() and at around line 1027 of search.php it determines that tax_query is user generated. So far so good. But since that block just loops over and appends the values to $tax_query, relevanssi_search() then assumes since $tax_query is an array that it will contain a ‘field’ property.

    How can I get around this?

Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter JoelStransky

    (@joelstransky)

    Here’s the complete error for context. This also happens on each line expecting access to $row['field']

    Notice: Undefined index: field in /var/www/html/wp-content/plugins/relevanssi/lib/search.php on line 92
    Call Stack
    #	Time	Memory	Function	Location
    1	0.0013	366632	{main}( )	.../index.php:0
    2	0.0019	366920	require( '/var/www/html/wp-blog-header.php' )	.../index.php:17
    3	0.3736	4512504	wp( )	.../wp-blog-header.php:16
    4	0.3736	4512552	WP->main( )	.../functions.php:955
    5	0.3754	4506576	WP->query_posts( )	.../class-wp.php:735
    6	0.3754	4506632	WP_Query->query( )	.../class-wp.php:617
    7	0.3754	4506632	WP_Query->get_posts( )	.../class-wp-query.php:3248
    8	0.3778	4541440	apply_filters_ref_array( )	.../class-wp-query.php:3007
    9	0.3778	4541440	WP_Hook->apply_filters( )	.../plugin.php:244
    10	0.3779	4542944	relevanssi_query( )	.../class-wp-hook.php:300
    11	0.3779	4542968	relevanssi_do_query( )	.../search.php:49
    12	0.3804	4544712	relevanssi_search( )	.../search.php:1322
    Plugin Author Mikko Saari

    (@msaari)

    What if you just add a field parameter to your tax_query? There should be one, after all.

    I can make Relevanssi copy WP behaviour and assume term_id if nothing is provided, but I guess it doesn’t hurt if you’re explicit with your parameters.

    Thread Starter JoelStransky

    (@joelstransky)

    imho it’s an inaccurate assumption to check if $tax_query is an array and assume it is a result of your last tax_query condition when the previous two also create arrays. But providing a field param myself should work for my needs.
    Thanks.

    Plugin Author Mikko Saari

    (@msaari)

    I’m no sure what you mean here, could you elaborate? Do you mean the $tax_query can contain something else than a tax_query? What do you mean with “previous two”?

    Thread Starter JoelStransky

    (@joelstransky)

    Thanks for your interest. I’ll attempt to explain in better detail. But first, my fix required two things.

    • 1. 'field' => 'term_id' was required for each of my user-created tax queries.
    • 2. I had to prevent any empty tax queries (arrays containing only a relation item) from being merged in.
    • Primarily, my issue is that 'field' is not a required parameter natively as it defaults to 'term_id' but Relevanssi requires it. Imho that should not be the case.

      As far as what I meant earlier about “previous two”: in relevanssi/lib/search.php ~line 1021, you start to assemble a $tax_query array. Within that block there are 4 conditions.

    1. Tax query is empty so remove it
    2. User-created tax query, loop over and push non-relation values to $tax_query
    3. Created by WP, loop over 'queries' and push to $tax_query
    4. attempt to collect categories and tags into $tax_query

    Conditions 2 & 3 fully assume that a 'field' param is present so when relevanssi_search() gets ahold of it, ~around line 89 there is merely a check for wether $tax_query is an array or not and if so, attempts to reference each $row‘s [‘field’] property. But the only way [‘field’] is guaranteed to exist is if the 4th condition was met. I haven’t tested condition 3 but I assume WP supplies the ‘field’ prop but there’s no guarantee a user will.

    I hope that clears it up. Thanks.

    Plugin Author Mikko Saari

    (@msaari)

    Making field default to term_id is an obvious fix, and I’ll switch to using terms to check whether a row is a relation row or an actual tax_query row, since terms is definitely going to appear.

    Your post has some code that breaks the layout of the page, by the way.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Undefined index: field’ is closed to new replies.