0

私は自分自身のために人生を頑張っていると思います。WordPressの検索フォームとカスタム分類のACFフィールドによる検索結果

私は3例に分割している私のウェブサイト上の検索フィールドがあります。私は、例えば、検索文字列を作成する私の結果ページで

<input type="text" name="part1" /> 
<input type="text" name="part2" /> 
<input type="text" name="part3" /> 

を:

$searchstring = $part1.'-'.$part2.'-'.$part3; 

プロセスは、その後にあります私のカスタムフィールドの値を持つデータベースを検索$searchstring

検索機能が見つかりましたhttps://gist.github.com/jserrao/d8b20a6c5c421b9d2a51そこには私が思っていることに近いhieve私は内のすべてを実装する方法は本当にわからないんだけど

私のデータは、おおよそ次のようになります。

(taxonomy) product_cat - (name) Category 1 - (custom field) gc_number - (value I need to search) 77-999-77 
(taxonomy) product_cat - (name) Category 2 - (custom field) gc_number - (value I need to search) 73-333-73 
(taxonomy) product_cat - (name) Category 3 - (custom field) gc_number - (value I need to search) 76-666-76 

は、私は、ユーザーにproduct_cat名前を表示する必要があります。

うまくいけば、これは何らかの意味があり、どんな助けでも大歓迎です! ACFドキュメント

$posts = get_posts(array(
    'numberposts' => -1, 
    'post_type'  => 'post', 
    'meta_key'  => 'color', 
    'meta_value' => 'red' 
)); 

この例から

答えて

-1

私はStack Exchange - Wordpress Developmentでこの質問をし、Kieran McClungから素晴らしい答えを得ました。彼の答えは私にとって完璧に働き、下にコピーされます。

<form role="search" method="get" id="acf-search" action="<?php site_url(); ?>"> 
... 

<input type="text" name="part1" /> 
<input type="text" name="part2" /> 
<input type="text" name="part3" /> 

<input type="submit" value="Search" /> 
</form> 


<?php 
// Results page 

// Might be worth checking to make sure these exist before doing the query. 
$part_1 = $_GET['part1']; 
$part_2 = $_GET['part2']; 
$part_3 = $_GET['part3']; 
$search_string = $part_1 . '-' . $part_2 . '-' . $part_3; 

// Get all product categories 
$categories = get_terms('product_cat'); 
$counter = 0; 

// Loop through categories 
foreach ($categories as $category) { 

    // Get custom field 
    $gc_number = get_field('gc_number', $category); 

    // If field matches search string, echo name 
    if ($gc_number == $search_string) { 
     // Update counter for use later 
     $counter++; 
     echo $category->name; 
    } 
} 

// $counter only increases if custom field matches $search_string. 
// If still 0 at this point, no matches were found. 
if ($counter == 0) { 
    echo 'Sorry, no results found'; 
} 
?> 
0

は「色」と呼ばれるカスタムフィールドは、「赤」の値を持つすべての記事を検索するために、引数を示しています。あなたの場合、あなたはmeta_keyにはgc_number、meta_valueには$searchstringを使用してください。次に、get_the_terms()を使用して、投稿の属するカテゴリ用語を定義することができます。

+0

こんにちは感謝します。私はそれを見てきました。私を投げているのは、この例ではポストとして設定されている問い合わせ済みのポストタイプです。しかし私のカスタムフィールドはタクソノミ(product_cat)に設定されています。タクソノミーやproduct_catなどに投稿を変更するだけですか? –

関連する問題