2016-07-01 6 views
1

別のカスタムポストタイプのACFリレーションシップフィールドを使用している「プロパティ」という名前のカスタムポストタイプを持つ1つのポストページの「関連する投稿」を表示します。ACFリレーションシップフィールドに基づいて関連ポストを表示

他の投稿タイプは「連絡先」で、「単一のプロパティ」投稿タイプでは、関係フィールドがそれを呼び出すようになっています。私はACF's documentation hereを理解しようとしていますが、私のコードがなぜ機能していないのかを実際に理解することはできませんでした。

ブローカーに基づいて関連するプロパティを表示する必要があります。私は完全にSQLステートメントとテーブルの結合を理解していません。

$properties = get_posts(array(
     'post_type'   => 'property', // Page Custom Post Type 
     'posts_per_page' => 6, 
     'meta_query'  => array(
      // 'relation' => 'AND', 
      // array(
       'key'  => 'contact', // Field name with 2nd custom post type, 'contact' 
       'value'  => '"' . get_the_ID() . '"', 
       'compare' => 'LIKE' 
      //) 
     ) 
    )); 
+0

ありがとうございました。 – Ishio

答えて

0

これは、ACFが配列を格納する方法に起因するという理由が分かりません。彼らのドキュメントは、彼らのケースでは機能しますが、ネスター配列のために私の中にいませんでした。

これは私のために働いたものです。

// This is the start of figuring out the array issue 
$contact = get_field('contact'); 
// This showed me the first array 
$contact_array = $contact[0]; 
// This showed me the ACF array and allowed me to return the ID 
$contact_ID = $contact_array->ID; 

$properties = get_posts(array(
    'post_type'   => 'property', 
    'posts_per_page' => 6, 
    'meta_query'  => array(
     'relation'  => 'AND', 
     array(
     'key'   => 'contact', 
     // Had to call the value like this as the array was nested like 
     // a:2:{i:0;s:3:"123";i:1;s:3:"321";} or something. 
     'value' => '"' . $contact_ID . '"', 
     'compare' => 'LIKE' 
    ) 
    ) 
)); 
関連する問題