others - PHP - 在ACF轉發器欄位上查詢

89 1

我想查詢一個ACF轉發器欄位,我有一個叫(在名為Book )的轉發器欄位,有一個名為Library (它連接到一個叫Library的自定義日誌類型)的關係欄位,我要查詢的就是這個欄位。

例子:'Library 1'被選中,返回:'Harry Potter 1'和'Harry Potter2'。

試驗(不工作)

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



时间: 原作者:

112 4


//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();



原作者:
...