問題描述

搜尋大約兩天來找到我的問題的答案,我終於註冊了這個 StackExchange 。 🙂

我的問題不簡單,所以我需要從一開始就開始。但是給你一個這個帖子的想法:同時我的外掛正在工作,但我不喜歡的程式碼。我認為有更好的方法來解決這個問題,所以我正在尋找可以幫助我很好的提示和最佳實踐的 wordpress 忍者。我真的試圖深入到 WordPress 的深處,但這很難。先謝謝你!


我的外掛應該做什麼

該外掛的使用者應該能夠使用短碼來顯示特定的帖子型別的搜尋表單。該外掛僅在包含此短程式碼的頁面上需要。

帖子型別是透過外掛設定頁面建立的。這種型別的每個帖子都有 acf 外掛的標題,內容和幾個自定義欄位。每個帖子型別都有一個短碼。搜尋表單有一個欄位來搜尋指定的帖子型別的所有字尾欄位。另外兩個可以用於透過兩個關鍵字來限制結果 (例如,位置,這是一個 acf 欄位) 。

結果透過 ajax 載入,並顯示在搜尋表單下方。


我已經做了

我試圖保持這個問題不要太大。所以我沒有指定每一個方面。以下應該正常工作:

  • 在外掛的 php 檔案中建立 post 型別。目前一個靜態的帖子型別是 enaugh 。

  • 建立一個列印搜尋表單的短程式碼,併為結果列印一個空容器。

  • Ajax 請求透過 javascript 工作,並提供預期的結果。


搜尋自定義欄位

這很難,但是我發現了一個工作的程式碼段,我明白了它在做什麼。這裡的問題是,我只想用特定的搜尋表單搜尋 acf 欄位。我不想觸控現有網站的搜尋。

首先,我嘗試檢查使用者所在的站點,並在修改搜尋查詢之前使用 if-statement 與 is_page() 。但是當我使用 ajax 時,這似乎對我來說不起作用

我的解決方法是在每個 ajax 搜尋查詢的開頭設定一個全域性變數。現在我在修改搜尋查詢之前檢查是否設定了此變數。在我的 ajax 函式結束時,我將此變數設定為 false 。是的,我認為有更好的方法來解決這個問題,但是我不知道如何

修改我的搜尋查詢的功能如下:


/**
 * Extend WordPress search to include custom fields
 *
 * http://adambalee.com
 */

/**
 * Join posts and postmeta tables
 *
 * http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_join
 */
function cf_search_join( $join ) {
    global $wpdb;
    //$cwlistpage=(is_page('list'));

    global $testcheck;
    $cwlistpage=$testcheck;

    if ( $cwlistpage ) {
        $join .=' LEFT JOIN '.$wpdb->postmeta. ' ON '. $wpdb->posts . '.ID = ' . $wpdb->postmeta . '.post_id ';
    }

    return $join;
}
add_filter('posts_join', 'cf_search_join' );

/**
 * Modify the search query with posts_where
 *
 * http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_where
 */
function cf_search_where( $where ) {
    global $pagenow, $wpdb;
    //$cwlistpage=(is_page('list'));

    global $testcheck;
    $cwlistpage=$testcheck;

    if ( $cwlistpage ) {
        $where = preg_replace(
            "/(s*".$wpdb->posts.".post_titles+LIKEs*('[^']+')s*)/",
            "(".$wpdb->posts.".post_title LIKE $1) OR (".$wpdb->postmeta.".meta_value LIKE $1)", $where );
    }

    return $where;
}
add_filter( 'posts_where', 'cf_search_where' );

/**
 * Prevent duplicates
 *
 * http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_distinct
 */
function cf_search_distinct( $where ) {
    global $wpdb;
    //$cwlistpage=(is_page('list'));

    global $testcheck;
    $cwlistpage=$testcheck;

    if ( $cwlistpage ) {
        return "DISTINCT";
    }

    return $where;
}
add_filter( 'posts_distinct', 'cf_search_distinct' );

透過自定義欄位限制結果

表單的所有欄位都是可選的。一個空的表單將返回此帖子型別的所有帖子。第一個欄位應該搜尋關鍵字的帖子的每個欄位。第二個和第三個欄位應該將結果限制為包含此關鍵字的結果。我用 if-statements 解決了這個問題,我知道必須有一個更好的解決方案。

/**
* Search with AJAX
*/
function cwlist_click_search() {
global $testcheck;
$testcheck = true;

$searchterm = $_POST['query'];
$searchlocation = $_POST['location'];
$searchdegree = $_POST['degree'];

// WP_Query arguments
$args = array (
    'post_type' => 'offers',
    'post_status' => 'publish',
    's' => $searchterm
);

$query = new WP_Query( $args );

ob_start();

// The Loop
if ( $query->have_posts() ) : ?>
  <br><br><p>
   <?php while ( $query->have_posts() ) : $query->the_post();
        if( ($searchlocation == NULL) || in_array(trim(strtolower($searchlocation)), array_map('strtolower', get_field('offer_location')))):
            if( ($searchdegree == NULL) || (trim(strtolower($searchdegree)) === strtolower(get_field('offer_degree')))):?>

     Titel: <?php the_title(); ?> <br>
     Abschluss: <?php the_field('offer_degree'); ?> <br>
     Ort: <?php the_field('offer_location'); ?> <br>
     Anbieter: <?php the_field('offer_provider'); ?> <br>
     <?php if(get_field('offer_subtitle')): ?>
     - <?php the_field('offer_subtitle'); ?> <br>
     <?php endif; ?>
     <br>

    <?php endif; endif; endwhile; ?>
    </p>
<?php else: ?>
    <!-- no posts found -->
    Keine Angebote gefunden
<?php endif;

// Restore original Post Data
wp_reset_postdata();

$content = ob_get_clean();

echo $content;
die();

$testcheck = false;
}
add_action( 'wp_ajax_nopriv_cwlist_click_search', 'cwlist_click_search' );
add_action( 'wp_ajax_cwlist_click_search', 'cwlist_click_search' );

WP 除錯

當我啟用外掛時,許多行如下所示出現在首頁:

Notice: Trying to get property of non-object in C:UsersCWaltDocumentsgitlabsteuerazubiwordpresswp-includesquery.php on line 4520 Notice: Trying to get property of non-object in C:UsersCWaltDocumentsgitlabsteuerazubiwordpresswp-includesquery.php on line 4522 Notice: Trying to get property of non-object in C:UsersCWaltDocumentsgitlabsteuerazubiwordpresswp-includesquery.php on line 4524 …

我啟用除錯來建立一個好的外掛。我可能做錯了什麼?


更多帖子型別

你知道一個好的方法讓使用者透過 wordpress 後端指定更多的帖子型別嗎?使用者應該能夠透過短碼為不同的郵件型別建立不同的搜尋頁面。


非常感謝您的閱讀。我感謝每一個小小的提示。

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。