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



原作者:
...