查询按ACF用户id过滤的自定义帖子Wordpress

问题描述 投票:0回答:1

我创建了一个名为“mypost”的帖子类型。使用 ACF。

尝试使用用户字段进行用户分配。字段名称 = usersacf

我只想显示在用户 Woocommerce 仪表板中分配给他的帖子。我用的是Elementor。我在那里创建了一个查询 ID。它是这个。但这不起作用。

感谢您对此的帮助。

这是我的代码。但不工作。

 function my_post_object_query( $args )
{
$args['author'] = get_current_user_id();

return $args;
}
add_filter('acf/fields/post_object/query/name=usersacf', 'my_post_object_query');

获取正确的代码

wordpress advanced-custom-fields custom-post-type custom-wordpress-pages acfpro
1个回答
0
投票

您似乎正在尝试过滤“mypost”帖子类型的用户字段中显示的帖子,以仅显示由当前用户在 WooCommerce 仪表板中使用 Elementor 创作的帖子。

您的代码尝试过滤名为“usersacf”的帖子对象字段的查询,以仅显示当前用户撰写的帖子。但是,acf/fields/post_object/query 挂钩并不特定于特定的帖子类型或上下文,因此在您的情况下它可能不会按预期运行。

为了实现您所需的功能,您需要确保在检索“mypost”帖子类型的帖子时以及在 WooCommerce 仪表板中专门修改查询。以下是实现这一目标的方法:

function filter_mypost_query_for_current_user( $query ) {
// Check if we are in the WooCommerce dashboard
if ( is_admin() && function_exists( 'is_wc_endpoint_url' ) && is_wc_endpoint_url() ) {
    global $pagenow;

    // Ensure we are on the correct page and dealing with "mypost" post type
    if ( $pagenow === 'edit.php' && isset( $_GET['post_type'] ) && $_GET['post_type'] === 'mypost' ) {
        // Modify the query to show posts authored by the current user
        $query->set( 'author', get_current_user_id() );
    }
}
}

add_action( 'pre_get_posts', 'filter_mypost_query_for_current_user' );
© www.soinside.com 2019 - 2024. All rights reserved.