Combine posts and postmeta

时间:2017-05-31 作者:ners10100100

我需要获取所有帖子ID、帖子标题和相应的meta\\u值,该值是自定义帖子类型的“特色”。我想我需要加入,但我不知道他们是怎么工作的。。谁能帮我一下吗?

2 个回复
SO网友:Hady Shaltout

您可以使用get\\u posts内置函数更改meta\\u键和meta\\u值

$args = array(
  \'meta_key\'         => \'YOUR_META_KEY\',
  \'meta_value\'       => \'featured\',
  \'post_type\'        => \'YOUR_POST_TYPE\',
  \'post_mime_type\'   => \'\',
  \'post_parent\'      => \'\',
  \'author\'     => \'\',
  \'author_name\'    => \'\',
  \'post_status\'      => \'publish\',
  \'suppress_filters\' => true 
);

$posts_array = get_posts( $args );

foreach ( $posts_array as $post ) : setup_postdata( $post ); ?>

<li>
    <span><?php echo esc_attr( $post->ID ); ?></span>

    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
<?php endforeach; 

// MUST Reset
wp_reset_postdata();
注意:不要修改本地foreach变量[$post]

有关更多详细信息,请参阅WordPress get_posts function

SO网友:inarilo
$args = array(
    \'post_type\'  => \'my_custom_post_type\',
    \'meta_query\' => array(
        array(
            \'key\'     => \'name_of_field_with_featured_as_value\',
            \'value\'   => \'featured\',
            \'compare\' => \'=\',
        ),
    ),
);
$query = new WP_Query($args);
if($query->have_posts()) :
    //usual loop code but with $query as above, note that post related functions are called as usual, e.g. simply the_title(), not $query->the_title()
endif;
结束

相关推荐

Wpdb->get_var始终返回0

我正在尝试运行以下语句:global $wpdb; $amt = $wpdb->get_var($wpdb->prepare(\"SELECT SUM(amount) as amt FROM wp_pay_table WHERE postid = %i\", $pid)); 。。。但是$amt总是显示为0。我已经检查过$pid是正确的值。我还在数据库中运行了sql语句,它返回了正确的值(应该是75)。我也尝试过get\\u row,但遇到了同样的问题。非常感谢您的帮助。