简体   繁体   English

具有多对多关系字段的 WP_Query 不返回结果

[英]WP_Query with many-to-many relationship field not returning results

I am writing a custom wordpress query that uses a relationship field called "related_places" to connect posts to multiple places (a custom post type).我正在编写一个自定义 wordpress 查询,该查询使用名为“related_places”的关系字段将帖子连接到多个位置(自定义帖子类型)。 If there is only one connected place in the relationship field my code works.如果关系字段中只有一个连接的地方,我的代码就可以工作。 If I add multiple connected places, the results will disappear entirely if I just use get_field.如果我添加多个连接的地方,如果我只使用 get_field,结果将完全消失。 If I put the places in an array the results still show, but it only returns the first place listed in the field.如果我将这些位置放在一个数组中,结果仍然会显示,但它只返回该字段中列出的第一个位置。 And if I use the ID field, its returns places whether or not they are connected.如果我使用 ID 字段,无论它们是否连接,它都会返回位置。 I cant find the middle ground where all related places show and all non-related places dont.我找不到所有相关地点都显示而所有不相关地点不显示的中间地带。 Can anyone see what I am missing?谁能看到我错过了什么?

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;       
}

I am not sure if its relevant, but the relationship field was created with the "PODS" plugin.我不确定它是否相关,但关系字段是使用“PODS”插件创建的。

If you are using PODS, it is not necessary to use WP_Query to get field values.如果您使用的是 PODS,则无需使用 WP_Query 来获取字段值。 According to the documentation , you can read all the IDs directly from the POD using the name that you have created根据文档,您可以使用您创建的名称直接从 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;
    }
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM