簡體   English   中英

在ACF轉發器字段上查詢

[英]Query on a ACF repeater field

我想查詢ACF轉發器字段。 我有一個稱為Library的轉發器字段(在CPT中稱為Book ),在這個轉發器中,我有一個稱為Library的關系字段(該字段與另一個稱為Library的自定義帖子類型相連)。 我想查詢的是此字段*。

由於用戶提供的唯一值(由於選擇而被選中),我希望恢復與該Library有關的所有書籍。

例如 :選擇“庫1”。 返回:'Harry Potter 1'和'Harry Potter 2'。

試用(無效)

function my_posts_where( $where ) {

$where = str_replace("meta_key = 'library_$", "meta_key LIKE 'library_%", $where);
return $where; } add_filter('posts_where', 'my_posts_where');
$library= $_GET['library'];

$v_args = array(
        'post_type'     =>  'book', 
        'meta_query'    =>  array(
                                array(
                                    'key'     => 'library_$_library',
                                    'compare' => '=', 
                                    'value'   => $library, 
                                ),
                            )
    ); $Query = new WP_Query($v_args);

    if($Query->have_posts()) :
        while($Query->have_posts()) : $Query->the_post();
            ...
        endwhile;

    else : 
        ...
    endif;

和這個

    $library= $_GET['library'];

    $v_args = array(
        'post_type'     =>  'book', 
        'meta_query'    =>  array(
                                array(
                                    'key'     => 'library',
                                    'value'   => $library,
                                    'compare' => 'LIKE', 
                                ),
                            )
    );

$Query = new WP_Query($v_args); 

我在互聯網上搜索,無法解決我的問題...

非常感謝。

-*:我也有一個稱為標簽的轉發器字段,其中有一個與標簽相關的分類字段。 我想展示所有帶有thig標簽的書。

這有點晚了,但是如果這對其他人有幫助的話,這對我有用:

//Since the changed behaviour of esc_sql() in WordPress 4.8.3, 
//cannot use the % character as a placeholder, hence need to alter 'where' close:
function my_posts_where( $where ) {
    $where = str_replace("meta_key = 'library_$", "meta_key LIKE 'library_%", $where);
    return $where;
}
add_filter('posts_where', 'my_posts_where');

$library_id = $_GET['library']; //get querystr var

$args = array(
     'post_type'      => 'book',
     'posts_per_page' => -1,
     'meta_query'     => array(
          array(
               'key' => 'library_$_library', // our repeater field post object
               'value' => '"'. $library_id .'"', //matches exactly "123", not just 123 - prevents a match for "1234"
               'compare' => 'LIKE'
          )
     )
);
$query = new WP_Query($args);

if ($query->have_posts()): 
    while ($query->have_posts()) : $query->the_post();
        echo $post->post_title.'<br>';
    endwhile; 
endif;

wp_reset_query();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM