2016-08-27 7 views
2

私はAjax search proプラグインとWooCommerceを使って先進的な商品を検索しています。Ajax search proアドバンスドオプション - カスタムフィールドと用語の組み合わせ

Ajax search proプラグインの設定には、[詳細オプション]タブがあり、投稿のタイトルと投稿の説明をカスタマイズできます。 '_price'post_title'を組み合わせAvengers 10$:あなたはAven...を入力して検索した場合、その後意志ショーあなたには、いくつかのカスタムフィールドwhith結果ショーをカスタマイズし、のようなものを得ることができ、高度なタブでAvengers ...

を引き起こす例えば
'。

私の問題は、カスタムフィールドをカスタムタクソノミと組み合わせることができないということです。たとえば、'post_title'カスタムフィールドをrelease_yearカスタム分類と組み合わせることはできません。この組み合わせのようなものはありません。アベンジャーズ2012

私は、カスタムフィールドとの間でいくつかの特別な組み合わせを使用したいと思います:

  • カスタム分類用語in this answerようrelease_year

  • カスタム分類の種類(カスタムカテゴリタイプ)tvshow_cat'product_cat'と同様である)
    tvshow_catタクソノミから最初の3つの関連する製品用語(昏睡区切り)の文字列を表示したいと考えています。

どうすればこの問題を解決できますか?

おかげ

+0

私は私の質問を編集しました –

答えて

3

あなたは、製品メタデータのカスタムフィールドで関連するフォーマットされた分類用語をコピーする関数を構築することができます。

1)2つの主要な機能によって使用されるサブ機能():ここにコメントコードであり、ここで

// Processing 'release_year' formatting in a string 

function process_release_year($post_id){ 

    $release_years_str = get_the_term_list($post_id, 'release-year', '', ','); 
    $release_years_arr = explode(',', $release_years_str); 
    $count = sizeof($release_years_arr); 
    $first_year = $release_years_arr[ 0 ]; 
    if ($count > 1) { 
     $last_year = $release_years_arr[ $count - 1 ]; 
     $releaseyear_as_text = ' (' . $first_year . ' - ' . $last_year . ')'; 
    } 
    elseif ($count == 1) $releaseyear_as_text = ' ' . $first_year; 
    else $releaseyear_as_text = ''; 

    return $releaseyear_as_text; 
} 


// Processing 'tvshow_cat' formatting in a string (3 coma separated terms in a string) 

function process_tvshow_cat($post_id){ 
    $description_terms = get_the_terms($post_id, 'tvshow_cat'); 
    $count = 0; $description_string = ''; 
    foreach ($description_terms as $description_term) { 
     $count++; 
     if($count < 4){ 
      $description_string .= $description_term; 
      if($count < 3) $description_string .= ', '; 
     } 
    } 
    return $description_string; 
} 


// The two custom fields creation mechanism 

function custom_fields_creation($post_id){ 

    // The release year 
    $test_cf1 = get_post_meta($post_id, 'release_year', true); 
    if(empty($test_cf1)) { 
     // if doesn't exist we create it 
     $release_year = process_release_year($post_id); 
     if(!empty($release_year)) 
      update_post_meta($post_id, 'release_year', $release_year); 
    } 

    // The TV show cat 
    $test_cf2 = get_post_meta($post_id, 'mov_description', true); 
    if(empty($test_cf2)) { 
     // if doesn't exist we create it 
     $description_mov = process_release_year($post_id); 
     if(!empty($description_mov)) 
      update_post_meta($post_id, 'mov_description', $description_mov); 
    } 
} 

一度だけを使用する機能される(データベースのバックアップを行います前)。この機能により、既存のすべての製品に2つの特別なカスタムフィールドが作成され、が作成されます。

// 1. FOR ALL EXISTING PRODUCTS ==> ==> ==> USE IT ONE TIME ONLY! 
add_action('woocommerce_init', 'product_custom_fields_bulk_action'); // To stop it, just comment this line 
function product_custom_fields_bulk_action(){ 

    // Get all published products 
    $products = get_posts(array(
     'post_type' => 'product', 
     'post_status' => 'publish', 
     'numberposts' => -1 
    )); 

    // Iterating each product 
    foreach($products as $product) 
     custom_fields_creation($product->id); 
} 

以下の機能は、そのカスタムフィールド新製品はを発表するたびに作成されます:

// 2. FOR "NEW CREATED" AND PUBLISHED PRODUCT 
add_action('transition_post_status', 'product_custom_fields_action', 10, 3); 
function product_custom_fields_action($new_status, $old_status, $post) { 
    $post_id = $post->ID; 
    if($old_status != 'publish' && $new_status == 'publish' && !empty($post_id) && in_array($post->post_type, array('product'))) 
     custom_fields_creation($post->ID); 
} 

このコードは、あなたのアクティブな子テーマやテーマにfunction.phpファイルに行くの...

あなたはあなたが望むように多くのカスタムフィールドのクローンを作成することができます...

このコードはテスト済みであり、動作します。

関連する問題