Skip Navigation

[Closed] TypeError thrown: urlencode(): Argument #1 ($string) must be of type string, arr

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 3 replies, has 2 voices.

Last updated by Nigel 1 year, 9 months ago.

Author
Posts
#2626621

I am trying to:
Carry out a search with array data included in the URL

Link to a page where the issue can be seen:
hidden link

I expected to see:
Search results

Instead, I got:
A TypeError error message is thrown.

TypeError thrown
urlencode(): Argument #1 ($string) must be of type string, array given

When I deactivate the toolset types plugin it removes the TypeError message so it appears to be related to some code in the plugin although I haven't been able to locate where the TypeError is being thrown.

This was working when we were using PHP 7.4 but it appears to have broken after we updated to PHP 8.1. We've had to remove the multiselect dropdowns on the expert search while we investigate this issue but these dropdowns were adding the array data to the search URL e.g. selecting multiple regional experience

The taxonomies being used for the expert search options have been set up using the toolset types plugin. This error also doesn't appear to be linked to the search results page content as I have removed all of the template code and the TypeError is still thrown.

Let me know if you need any further information.

Thanks

#2626643

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi Gavin

Could you please verify what the various parameters in the search URL refer to, specifically do they relate to custom fields or to custom taxonomies?

And how were the fields/taxonomies added to the search filters, as dropdowns, checkboxes?

We can set up a test site with something similar and check it against PHP 8.1.

#2626719

Hi Nigel,

Thanks for the reply,

The various parameters in the search URL refer to custom taxonomies set up in Toolset Types. These are used with a tax_query in WP_Query to select the custom post types that match the taxonomies/search.

I have included the WP_Query code below:

      $search_box = $_GET['s'];
      $expert_service = $_GET['expert-service'];
      $regional_experience = $_GET['regional-experience'];
      $sector_experience = $_GET['sector-experience'];
      $language = $_GET['language'];
      $location = $_GET['location'];

      $tax_query = array('relation' => 'AND');
      if (!empty($expert_service))
      {
          $tax_query[] =  array(
                  'taxonomy' => 'expert-service',
                  'field' => 'slug',
                  'terms' => $expert_service
              );
      }
      if (!empty($regional_experience))
      {
          $tax_query[] =  array(
                  'taxonomy' => 'regional-experience',
                  'field' => 'slug',
                  'terms' => array_values($regional_experience),
                  'operator' => 'AND',
              );
      }
      if (!empty($sector_experience))
      {
          $tax_query[] =  array(
                  'taxonomy' => 'sector-experience',
                  'field' => 'slug',
                  'terms' => array_values($sector_experience),
                  'operator' => 'AND',
              );
      }
      if (!empty($language))
      {
          $tax_query[] =  array(
                  'taxonomy' => 'language',
                  'field' => 'slug',
                  'terms' => array_values($language),
                  'operator' => 'AND',
              );
      }
      if (!empty($location))
      {
          $tax_query[] =  array(
                  'taxonomy' => 'location',
                  'field' => 'slug',
                  'terms' => $location
              );
      }
        
        add_filter( 'posts_orderby' , 'posts_orderby_lastname' );
       
        //Query for meta_query and tax_query
        $q1 = new WP_Query(
          array(
            'post_type' => 'expert-post',
            'posts_per_page' => -1,
            'meta_query' => array(
                'relation' => 'OR',
                array(
                  'key' => 'expert_keywords',
                  'value' => $search_box,
                  'compare' => 'LIKE'
                )
            ),
            'orderby' => array(
                'posts_orderby_lastname' => 'ASC',

            ),
            'tax_query' => $tax_query,
            'fields' => 'ids'
          )
        );

        //A separate query for 's' parameter. You can't use the 's' parameter with meta_query.
        $q2 = new WP_Query(
          array(
            'post_type' => 'expert-post',
            'posts_per_page' => -1,
            'orderby' => array(
                'posts_orderby_lastname' => 'ASC',

            ),
            's' => $search_box,
            'tax_query' => $tax_query,
            'fields' => 'ids'
          ) 
        );

        $allTheIDs = array_unique(array_merge($q1->posts, $q2->posts));

        if(empty($allTheIDs)){
          $allTheIDs = array(0);
        }

      //Merge the two WP_Query results
      $query = new WP_Query(array(
        'post_type' => 'expert-post',
        'post__in' => $allTheIDs,
        'posts_per_page' => -1,
        'orderby' => array(
          'posts_orderby_lastname' => 'ASC',
        ),
      ));

The fields/taxonomies added to the search filters are added using dropdowns. There was a javascript plugin added that allowed you to multi select in the dropdowns but this was removed due to the TypeError. I believe it worked similar to using checkboxes so multiple options can be added to the GET request. e.g. &regional-experience[]=asia&regional-experience[]=americas

Also, the expert search isn't actually using the default WordPress search template so this could possibly be part of the problem.

In search.php we have the following code:

<?php
$search_refer = $_GET["site_section"];
$search_refer_product = $_GET["post_type"];
$search_refer_knowledge = $_GET["post_knowledge"];
if ($search_refer_product == 'expert') { load_template(TEMPLATEPATH . '/search-expert-post.php'); }
else if ($search_refer == 'site-search') { load_template(TEMPLATEPATH . '/search-site.php'); }
else if ($search_refer_knowledge == 'knowledge-document') { load_template(TEMPLATEPATH . '/search-knowledge-document.php'); }
?>

When the expert search is submitted it is sent with $_GET["post_type"] set as "expert" and a different template is loaded in '/search-expert-post.php'. This template/page then includes the WP_Query code above.

Let me know if you need any further details.

Thanks

#2626725

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Sorry, I misunderstood.

I thought this related to a Views custom search, but I see that it is a custom implementation.

It's not obvious that it has anything to do with Toolset, other than that you say the taxonomies are registered with Types (but that is functionally equivalent to registering them directly with the core register_taxonomy function anyway).

Do you have full details of the TypeError error?

Your debug log should include more information about the error, at least including the file and line number where the error occurs, but possibly also including the stack trace that leads to that code.

The topic ‘[Closed] TypeError thrown: urlencode(): Argument #1 ($string) must be of type string, arr’ is closed to new replies.