自定义WP_QUERY的结果出现在第404页(但找到结果!)

时间:2020-05-07 作者:Floh

我写了一个自定义搜索页面。它工作得很好,然后我添加了额外的更改,现在我得到了404结果问题。即使我进行了恢复(我有备份)并清除了缓存,问题仍然存在。我不确定这是否是因为我今天运行的Wordpress更新。

所以我想找出这里有什么错,因为我不知道为什么我得到了“404未找到”-页面上有“对不起,keine Beiträge gefunden”(意思是“对不起,找不到帖子”),但我看到了结果(所以它实际上很好!)。由于404页,我的结果页(正如我所说的那样)看起来破了。

我刚刚发现:如果我将“Name”字段保持为空,那么页面结果就可以了。但当我输入name(在页面标题中搜索)时,页面就被找到了,但我得到了404页。

这是我的密码,也许我监督了一些重要的事情?

<?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();
?>
这里是函数中的部分。子主题php:

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;
提前感谢您!

最佳regardsFloh

1 个回复
SO网友:Floh

我发现它是因为我使用了一个名为=“name”的输入,这导致了这个问题。我相信“name”是wordpress/php中的一个关键词。我把“name”换成了“personname”,然后它就变成了魅力。

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