【问题标题】:Loop throuh all members of site in WordPress/WooCommerce循环访问 WordPress/WooCommerce 中的所有站点成员
【发布时间】:2017-11-28 19:34:44
【问题描述】:

我想显示我网站的所有成员。

据我了解 WooCommerce Membership 插件,成员存储为自定义帖子类型 (https://docs.woocommerce.com/document/woocommerce-memberships-data-structure/#section-4),我可以使用 get_posts() 获取它们。

我尝试使用以下代码:

<ul class="my-5">
<?php
global $post;
$args = array( 'post_type' => 'wc_user_membership' );
$posts = get_posts( $args );
foreach ( $posts as $post ) : 
    setup_postdata( $post ); ?>
    <li><?php echo $post->ID; ?></li>
<?php endforeach; wp_reset_postdata(); ?>
</ul>

很遗憾没有任何结果。 我的代码有什么问题吗?

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    更新:

    尝试在您的$args 数组中添加'numberposts' =&gt; -1'post_status' =&gt; 'wcm-active',这样您的代码将是:

    <ul class="my-5">
    <?php
    $posts = get_posts( array( 
        'post_type' => 'wc_user_membership', 
        'post_status' => 'wcm-active',
        'numberposts' => -1,
    ) );
    
    foreach ( $posts as $post ) :
        setup_postdata( $post ); ?>
        <li><?php echo $post->ID; ?></li>
    <?php endforeach; wp_reset_postdata(); ?>
    </ul>
    

    您不需要global $post;,因为您在foreach 循环中使用$post

    经过测试并且有效。

    【讨论】:

    • 这没有帮助:(我猜帖子类型 wc_user_membership 在前端不可用。我尝试使用适用于其他帖子类型的功能来获取此帖子类型的所有作者。在这种情况下也没有显示任何结果。
    • 在桌子上?!
    • 好的。我会再次检查我的网站
    • 已更新并正在工作......您还需要指定 post_status(或多个时数组中的状态)
    • 就是这样。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-07
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 2016-07-17
    相关资源
    最近更新 更多