wordpress - 我希望 get_posts 只返回登录用户的帖子
问题描述
这是我的代码:
我只想返回登录用户的帖子。
我在 WordPress 重力表单中填充下拉表单。
$current_user = _wp_get_current_user();
function populate_posts( $form) {
foreach ( $form['fields'] as &$field ) {
if ( $field->type != 'select' || strpos( $field->cssClass, 'populate-posts' ) === false ) {
continue;
}
$posts = get_posts( 'post_type=credit cards&numberposts=-1&post_status=publish&author=$current_user' );
$choices = array();
foreach ( $posts as $post ) {
$choices[] = array( 'text' => $post->post_title, 'value' => $post->post_title );
}
$field->placeholder = 'Select Credit Card';
$field->choices = $choices;
}
解决方案
据我了解,您的问题是获取“当前用户的帖子”。然后这是下面的代码。
1)您需要将作者 ID(当前用户 ID)作为参数传递。
$current_user = wp_get_current_user();
$args = array(
'author' => $current_user->ID,
'orderby' => 'post_date',
'order' => 'ASC',
);
2)通过以上参数发布如下:
$current_user_posts = get_posts( $args );
我希望这可以帮助你。
推荐阅读
- reactjs - 使用 Hello.js 在没有登录会话的情况下反应 Microsoft Outlook 日历数据
- python - 消除数据框中的重复行并保留具有特定字符串值的行
- react-native - flatlist 分页使应用程序在 android 上非常慢
- javascript - 在地图功能中为一个项目反应切换类(但是当另一个项目将被从另一个项目中删除时)
- javascript - 没有猎犬的 Typeahead 返回多个错误
- python - 如何将 unicode 转换为数据框列的字符串?
- python - 如何将 matplotlib 中的默认字体设置为 Helvetica?
- python - 使用python从API获取JSON数据时获取keyerror
- scala - 将嵌套的 spark Row 转换为嵌套的地图 Map[String, Any](反之亦然)
- ruby-on-rails - ActiveRecord/Devise 未提供“已采取错误”