1

I am creating a custom search form which will search a custom post type called recipes. and it has a load of taxonomies.

Now I output these taxonomy's terms into checkboxes which are grouped by the taxonomy name.

The issue is on the input:

<!-- language: lang-html -->

  <label><input type="checkbox" name="epx_recipes_season[]" value="autumn">Autumn</label>
  <label><input type="checkbox" name="epx_recipes_season[]" value="spring">Spring</label>
  <label><input type="checkbox" name="epx_recipes_season[]" value="summer-3">Summer</label>
  <label><input type="checkbox" name="epx_recipes_season[]" value="winter-3">Winter</label>

  <label><input type="checkbox" name="epx_recipes_course[]" value="breakfast-3">Breakfast</label>
  <label><input type="checkbox" name="epx_recipes_course[]" value="brunch-2">Brunch</label>
  <label><input type="checkbox" name="epx_recipes_course[]" value="dessert">Dessert</label>
  <label><input type="checkbox" name="epx_recipes_course[]" value="dinner">Dinner</label>
  <label><input type="checkbox" name="epx_recipes_course[]" value="light-meals-2">Light Meals</label>
  <label><input type="checkbox" name="epx_recipes_course[]" value="lunch-3">Lunch</label>

  <label><input type="checkbox" name="epx_recipes_requirements[]" value="diary-free">Diary free</label>
  <label><input type="checkbox" name="epx_recipes_requirements[]" value="egg-free">Egg Free</label>
  <label><input type="checkbox" name="epx_recipes_requirements[]" value="gluten-free-3">Gluten Free</label>
  <label><input type="checkbox" name="epx_recipes_requirements[]" value="nut-free">Nut Free</label>
  <label><input type="checkbox" name="epx_recipes_requirements[]" value="shellfish-free">Shellfish free</label>
  <label><input type="checkbox" name="epx_recipes_requirements[]" value="vegitarians">vegitarians</label>

  <label><input type="checkbox" name="epx_recipes_cuisines[]" value="american">American</label>
  <label><input type="checkbox" name="epx_recipes_cuisines[]" value="british-3">British</label>
  <label><input type="checkbox" name="epx_recipes_cuisines[]" value="greek-3">Greek</label>
  <label><input type="checkbox" name="epx_recipes_cuisines[]" value="irish">Irish</label>
  <label><input type="checkbox" name="epx_recipes_cuisines[]" value="italian">Italian</label>
  <label><input type="checkbox" name="epx_recipes_cuisines[]" value="mexican-3">Mexican</label>
  <label><input type="checkbox" name="epx_recipes_occastions[]" value="birthdays">Birthdays</label>
  <label><input type="checkbox" name="epx_recipes_occastions[]" value="christmas">Christmas</label>
  <label><input type="checkbox" name="epx_recipes_occastions[]" value="easter-3">Easter</label>
  <label><input type="checkbox" name="epx_recipes_occastions[]" value="fathers-day-2">Fathers Day</label>
  <label><input type="checkbox" name="epx_recipes_occastions[]" value="mothers-day-2">Mothers day</label>
  <label><input type="checkbox" name="epx_recipes_occastions[]" value="valentines">Valentines</label>

The issue comes from the name they have to keep the grouped together they have a [] so when posted it goes as array which i can then parse. this issue is WordPress is reading the form straight away and outputting the following error message.

Notice: Array to string conversion in /home/sites/epixdev1.co.uk/public_html/htk2014/wp-includes/query.php on line 1858

Warning: preg_split() expects parameter 2 to be string, array given in /home/sites/epixdev1.co.uk/public_html/htk2014/wp-includes/query.php on line 1867

Notice: Array to string conversion in /home/sites/epixdev1.co.uk/public_html/htk2014/wp-includes/query.php on line 1858

Warning: preg_split() expects parameter 2 to be string, array given in /home/sites/epixdev1.co.uk/public_html/htk2014/wp-includes/query.php on line 1867

Notice: Trying to get property of non-object in /home/sites/epixdev1.co.uk/public_html/htk2014/wp-content/plugins/wordpress-seo/frontend/class-opengraph.php on line 524

Here the code that makes the form

<!-- language: lang-php -->

//Will be used to output the advanced search box
function epix_recipes_search(){
    //Need to run each different type of taxonmies
    //How Many there are
    $noTax = 6;

    //The Taxonomies
    $taxonomies = array('epx_recipes_season','epx_recipes_course','epx_recipes_requirements','epx_recipes_cuisines','epx_recipes_occastions','epx_recipes_typeofcooking');

    //need the basic output varible 
    $output = "";

    //Need to build a basic form
    $output .= '<form method="post" id="searchform" action="/recipes/">'."\r\n";
    //Now a Basic Search box
    $output .= '    <h2>Keyword Search</h2>'."\r\n";
    $output .= '    <input type="text" class="field" name="s" id="s" size="19" placeholder="Search" />'."\r\n";

    //Now need to Show the Advance Search Area
    $output .= '    <h2>Advanced Filters</h2>'."\r\n";
    //Now need to run the system for each Taxonomies
    foreach ( $taxonomies as $tax ) {
        //Need to get the name of each TAX
        //What Taxonomy we want to check
        $args=array(
          'name' => $tax
        );
        $outputtype = 'objects'; // or objects
        $taxonomieslist=get_taxonomies($args,$outputtype); 
        if  ($taxonomieslist) {
            foreach ($taxonomieslist as $taxonomiesone ) {
              $output .= '  <h3>'. $taxonomiesone->labels->name. '</h3>'."\r\n";
            }
        }

        //now get the terms in the Taxonomies
        $output .= epix_recipes_taxterms($tax);
    }

    //now need to end the form with the correct sytack

    //First a submit button
    $output .= '    <input type="submit" class="submit" name="submit" id="searchsubmit" value="submit" />'."\r\n";
    //Now the closing for tag
    $output .= '</form'."\r\n";

    //Need to return the form
    return $output;
   }

  //This will get the taxonomies terms
  function epix_recipes_taxterms($tax){

    //Need to get each term and return it
    $returnoutput = "";
    $terms = get_terms($tax,'hide_empty=0');
    if ( !empty( $terms ) && !is_wp_error( $terms ) ){
        //$returnoutput .= "  <select name=".$tax.">"."\r\n";
        $i = 0;
        foreach ( $terms as $term ) {
            $returnoutput .= '      <label><input type="checkbox" name="'.$tax.'[]" value="'.$term->slug.'">' . $term->name . '</label>'."\r\n";
            //$i++;['.$i.']
        }
        //$returnoutput .= "  </select>"."\r\n";
    }
    return $returnoutput;
   }

If anyone can help me work out why this is happening, or a way to stop WordPress giving out these errors that would be really helpful.

Yes I have WordPress debug on!

0

2 Answers 2

1

Here's an option.
Give a more specific name e.g. custom_search_form to your submit button:

(...)
//First a submit button
$output .= '<input type="submit" class="submit" name="custom_search_form" id="searchsubmit" value="submit" />'."\r\n";

Then you can add this to your functions.php file:

add_action( 'parse_request', 'my_custom_search_form' );
function my_custom_search_form($query) {
  //return if it not our custom search form
  if ( ! isset( $query->query_vars[ 'custom_search_form' ] ) )
    return $query;

  //handle form here
}

Also, don't forget to set the query_var to true when registering your taxonomy:

'query_var'         => true,
3
  • I tried that but it still bringing up them errors, it like it reading it no matter what i do. Oct 1, 2014 at 13:28
  • I've edited my answer to change the action to "parse_request". Hope that it works for you. Oct 1, 2014 at 14:00
  • Sadly tried that did not resolve that issue, :/ Oct 2, 2014 at 6:15
0

Some notes. They may not solves all your issues but they might help.

In epix_recipes_search(), you're using get_taxonomies() as if it were get_taxonomy() in your foreach loop.

You can replace all of this:

$args=array(
    'name' => $tax
);
$outputtype = 'objects'; // or objects
$taxonomieslist=get_taxonomies($args,$outputtype);

with this:

$taxonomieslist = get_taxonomy( $tax )

The $taxonomieslist is a bit of a misnomer since it only contains a single taxonomy object (I'd rename it accordingly, maybe $tax_object). Therefore, you can also drop all of this:

if ($taxonomieslist) {
    foreach ($taxonomieslist as $taxonomiesone ) {
        $output .= '  <h3>'. $taxonomiesone->labels->name. '</h3>'."\r\n";
    }
}

And replace it with this:

$output .= '  <h3>'. $taxonomieslist->labels->name. '</h3>'."\r\n";

It also looks like you're missing a closing ">" on your </form> tag.

1
  • hey, No that did not fix them errors. however has helped clean up the code. many thanks Oct 2, 2014 at 8:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.