0 レビュー
1 回答
php-多対多の関係フィールドを持つWP_Queryが結果を返さない
「related_places」という関係フィールドを使用して投稿を複数の場所に接続するカスタムワードプレスクエリを作成しています(カスタム投稿タイプ)。リレーションシップフィールドに接続されている場所が1つしかない場合、私のコードは機能します。複数の接続された場所を追加した場合、get_fieldを使用すると、結果は完全に消えます。場所を配列に入れても結果は表示されますが、フィールドにリストされている最初の場所のみが返されます。また、IDフィールドを使用すると、接続されているかどうかに関係なく、IDフィールドが返されます。関連するすべての場所が表示され、関連しないすべての場所が表示されない中間点を見つけることができません。誰かが私が欠けているものを見ることができますか?
function related_places(){
$rplace = get_field( 'related_places',false,false );
/*If I use $rplace in the WP_query, the results disapear entirely if there is more than one related "place".
It works if there is only one connection)*/
$rplace_array = $rplace[0];
$rplace_ID = $rplace_array->ID; /*returns all places whether or not they are connected, there is no filter*/
$query = new WP_Query(array(
'post_type' => 'gd_place',
'posts_per_page' => 6,
'post__in' => $rplace_array, /*only the first related record is returned using this*/
'orderby'=> 'post__in'
));
if($query->have_posts()) :
while($query->have_posts()) :
$query->the_post() ;
$result .= '<div class="place-item">';
$result .= '<div class="place-image">' . get_the_post_thumbnail() . '</div>';
$result .= '<div class="place-name">' . get_the_title() . '</div>';
$result .= '</div>';
endwhile;
wp_reset_postdata();
endif;
return $result;
}
関連性があるかどうかはわかりませんが、リレーションシップフィールドは「PODS」プラグインを使用して作成されました。
わからない
0
レビュー
答え :
解決策:
PODSを使用している場合、フィールド値を取得するためにWP_Queryを使用する必要はありません。 ドキュメントによると、作成した名前を使用して、PODからすべてのIDを直接読み取ることができます
<?php
function related_places(){
//get Pods object for current post
//Replace pod_name with your pod name
$pod = pods( 'pod_name', get_the_id() );
//get the value for the relationship field
$related = $pod->field( 'related_places' );
//loop through related field, creating links to their own pages
//only if there is anything to loop through
$result = '';
if ( ! empty( $related ) ) {
foreach ( $related as $rel ) {
//get id for related post and put in ID
//for advanced content types use $id = $rel[ 'id' ]
$id = $rel[ 'ID' ];
$result .= '<div class="place-item">';
$result .= '<div class="place-image">' . get_the_post_thumbnail( $id ) . '</div>';
$result .= '<div class="place-name">' . get_the_title( $id ) . '</div>';
$result .= '</div>';
} //end of foreach
} //endif ! empty ( $related )
return $result;
}
?>
わからない
同様の質問
私たちのウェブサイトで同様の質問で答えを見つけてください。