针对特定自定义字段值发布帖子

时间:2015-04-14 16:26:52

标签: php

我希望发布具有自定义字段值的帖子' Rap'针对自定义文字字段'音乐类型'

$artists = array( 'posts_per_page' => 50, 'offset'=> 0, 'meta_key' => 'music-type', 'meta_value' => 'Rap', 'order'=>'ASC' );
$artists_lst = get_posts( $artists); foreach ($artists_lst as $post ) : setup_postdata( $post );
$post_id=$post->ID;
<li><a href="#"><?php echo $post_id; ?></a></li>
<?php endforeach; ?>
</ul>

2 个答案:

答案 0 :(得分:0)

https://codex.wordpress.org/Function_Reference/get_post_custom_values我会尝试:

$artists = array( 'posts_per_page' => 50, 'offset'=> 0, 'meta_key' => 'music-type', 'meta_value' => 'Rap', 'order'=>'ASC' );
$artists_lst = get_posts( $artists);
foreach ($artists_lst as $post ) : 
  setup_postdata( $post );
  $post_id=$post->ID;
  if( get_post_custom_values( 'music-type',  $post_id) == "Rap" ):?>
  <li><a href="#"><?php echo $post_id; ?></a></li>
  <?php endif;
endforeach; ?>

函数信息页面报告它返回一个数组,因此您可能需要在比较之前将其推送到变量中。像:

$music_type = get_post_custom_values( 'music-type',  $post_id);
if($music_type[0] == "Rap"):?>

希望有所帮助。

答案 1 :(得分:0)

解决了问题,使用get_pages而不是get_posts。

相关问题