0

I wrote a custom search page. It was working well then I added additional changes, now I got 404-Result-problem. The problem keep there even I did revert (I have backup) and I cleared the cache. I'm not sure if it's because of Wordpress update i ran today.

So I want to find out what wrong here since I don't know why I got "404 Not Found"-Page with "Sorry, keine Beiträge gefunden" (which means "Sorry, no posts found") but there I see the result (so its actually fine!). Because of 404-Page my Result-Page (as I said its found) looks broken.

I just found out: If I keep the "Name" field empty, then the page result is fine. But as soon I enter name (which searchs in title of pages) pages are found but I got 404-Page.

Here my code, maybe I oversaw something important?

<?php

class FlohSearch
{

    public function __construct()
    {
        add_action('init', array($this, 'init'));
        add_shortcode('floh_search', array($this, 'shortcode_handler'));
    }

    public function init()
    {
        if (!empty($_POST['nonce_custom_form']))
        {
            if (!wp_verify_nonce($_POST['nonce_custom_form'], 'handle_custom_form'))
            {
                die('You are not authorized to perform this action.');
            } else
            {
                $error = null;
                if (empty($_POST['name']) && empty($_POST['workingLanguage']) && empty($_POST['workingArea']))
                {
                    $error = new WP_Error('empty_error', __('Please enter search request.', 'floh'));
                    wp_die($error->get_error_message(), __('FlohSearch Error', 'floh'));
                }
                else
                {
                  $search_names = explode(' ', $_POST['name']);
                  $args = array(
                    'post_type' => 'page',
                    'post_parent' => $_POST['district'],
                    'title_filter' => $_POST['name'],
                    'meta_query' => array(
                      'relation' => 'AND',
                      array(
                        'key' => 'arbeitssprache',
                        'value' => $_POST['workingLanguage'],
                        'compare' => 'LIKE'
                      ),
                      array(
                        'key' => 'fachgebiet',
                        'value' => $_POST['workingArea'],
                        'compare' => 'LIKE'
                      )
                    )
                  );
                  add_filter('posts_where', 'title_filter',10,2);
                  $query = new WP_Query( $args );
                  remove_filter('posts_where', 'title_filter',10);

                  // The Loop
                  if( $query->have_posts() ) {
                    echo '<div id="flohresult">';
                    while ( $query->have_posts() ) {
                      $query->the_post();
                      $link = wp_get_shortlink();
                      echo '<div id="item">';
                      echo '  <div id="item-header"><h3><a href="'.$link.'">'.get_the_title().'</a></h3>';
                      echo '    <div id="item-row">';
                      echo '      <div id="item-thumbnail">';
                      echo '        <a href="'.$link.'">';
                      the_post_thumbnail('thumbnail');
                      echo '        </a></div>';
                      echo '        <div id="item-excerpt">';
                      echo '          <h4>Wohnort:</h4>'.get_post_meta($query->post->ID, 'wohnort', true);
                      echo '          <h4>Arbeitssprachen:</h4>'.get_post_meta($query->post->ID, 'arbeitssprache', true);
                      echo '          <h4>Fachgebiete:</h4>'.get_post_meta($query->post->ID, 'fachgebiet', true);
                      echo '        </div>';
                      echo '      </div>';
                      echo '  </div>';
                      echo '</div>';
                    }
                    echo '</div>';
                  } else {
                    echo '<div id="flohresult">';
                      echo '<h3>Kein Ergebnis mit folgende Suchfilter:</h3>';
                      echo '<p>Name: '.$_POST['name'].'</p>';
                      echo '<p>Bezirk: ';
                      switch($_POST['district'])
                      {
                        case 65:
                          echo 'Mittelfranken';
                          break;
                        case 361:
                          echo 'Niederbayern';
                          break;
                        case 363:
                          echo 'Oberbayern';
                          break;
                        case 355:
                          echo 'Oberfranken';
                          break;
                        case 359:
                          echo 'Oberpfalz';
                          break;
                        case 365:
                          echo 'Schwaben';
                          break;
                        case 357:
                          echo 'Unterfranken';
                          break;
                        default:
                          echo 'Ausserhalb Bayerns';
                      }
                      '</p>';
                      echo '<p>Arbeitssprache: '.$_POST['workingLanguage'].'</p>';
                      echo '<p>Fachbereich: '.$_POST['workingArea'].'</p>';
                    echo '</div>';
                  }
                }
            }
        }
    }

    function shortcode_handler($atts)
    {
        return "<form method='post' action=''>
            <p>
              Name:</br>
              <input name='name' type='text' value='' />
            </p>
            <p>
              Bezirk:</br>
              <select name='district'>
                <option value='65'>Mittelfranken</option>
                <option value='361'>Niederbayern</option>
                <option value='363'>Oberbayern</option>
                <option value='355'>Oberfranken</option>
                <option value='359'>Oberpfalz</option>
                <option value='365'>Schwaben</option>
                <option value='357'>Unterfranken</option>
                <option value='421'>Ausserhalb Bayerns</option>
              </select>
            </p>
            <p>
              Arbeitssprache:</br>
              <input name='workingLanguage' type='text' value='' />
            </p>
            <p>
              Fachgebiet:</br>
              <input name='workingArea' type='text' value='' />
            </p>
            " . wp_nonce_field('handle_custom_form', 'nonce_custom_form') . "
            <input id='button' type='submit' value='Submit'/>
            </form>";
    }
}
$FlohSearch = new FlohSearch();
?>

And here the part in function.php of child-theme:

function title_filter($where, $wp_query)
{
    global $wpdb;
    if($search_term = $wp_query->get( 'title_filter' )){
        $search_term = $wpdb->esc_like($search_term);
        $search_term = ' \'%' . $search_term . '%\'';
        $title_filter_relation = 'AND';
        $where .= ' '.$title_filter_relation.' ' . $wpdb->posts . '.post_title LIKE '.$search_term;
    }
    return $where;

Thank you in advance!

Best regards Floh

0

I found out its because I used a Input with name='name' which caused this issue. I believe the name 'name' is a keyword in wordpress/php. I replaced the 'name' to 'personname' then it works as charm.

...
 <input name='personname' type='text' value='' />
...
$_POST['gsdname']   // multiple time in my code
...

Best regards Floh

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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