Wordpress元查询ACF中继器字段发布对象-不起作用

时间:2018-12-17 16:14:59

标签: php wordpress custom-post-type advanced-custom-fields meta-query

我在WP网站上有3种自定义帖子类型,包括书籍,有声读物和作者。

在作者页面上,我想显示该作者的相关书籍/有声读物。

在“书籍/有声读物”页面上,有一个“转发器”字段,允许用户选择一个或多个“作者”作为发布对象(以使他们可以从CPT中选择)。

Repeater字段='author_repeater'。

子字段='作者'。

下面的代码是我尝试的元查询,但似乎没有用。我怀疑这是因为我没有正确访问post对象(不确定它是否可能)。

有什么想法吗?

<?php

    $current_author = get_the_title();

    function my_posts_where( $where ) {

        $where = str_replace("meta_key = 'author_repeater_$", "meta_key LIKE 'author_repeater_$", $where);

        return $where;
    }

add_filter('posts_where', 'my_posts_where');


    $args = array(
                    'post_type' => 'books', //array('books','audiobooks')
                    'post_status' => 'publish',
                    'posts_per_page' => 40,
                    'meta_query' => array(
                        array(
                            'key' => 'author_repeater_$_author',
                            'compare' => 'LIKE',
                            'value' => $current_author
                        )
                    )                       
    );

$books_by_author = new WP_Query( $args );

if ( $books_by_author->have_posts() ): while ( $books_by_author->have_posts() ): $books_by_author->the_post();

?>

<div class="card">
        <div class="white_frame">
            <h4 class="card__title"><?php the_title(); ?></h4>
            <a href="<?php the_permalink(); ?>">
                <?php the_post_thumbnail(array('class' => 'card__img img')); ?>
            </a>
            <a href="<?php the_permalink(); ?>">
                <div class="card__btn">View title</div>
            </a>
        </div>
    </div>

<?php
endwhile;
endif;
?>

0 个答案:

没有答案
相关问题