【问题标题】:How to retrieve all custom fields for a specific post type added dynamically如何检索动态添加的特定帖子类型的所有自定义字段
【发布时间】:2019-03-23 06:20:58
【问题描述】:

我正在开发这个 WordPress 演示插件,我想在其中显示/显示这 3 种帖子类型(PostPageCustom Post Types)中的每一种的自定义字段。不管有多少。

让我尽可能地进一步说明这个过程。

  1. 帖子类型:
    1.1 帖子的所有自定义字段

  2. 页面帖子类型:
    2.1 页面的所有自定义字段

  3. 自定义帖子类型:
    3.1 自定义帖子类型的所有海关字段单独(slug)

我正在寻找内置或定制的解决方案。

谢谢!任何方向都受到高度赞赏。

【问题讨论】:

    标签: php wordpress custom-post-type


    【解决方案1】:

    终于用小方向弄明白了。

    这是我实现它的方法。

    global $namespace_get_options; // replace with your assigned get_options variable.
    
    // Get all post types
    
    $post_args = array(
    
        'public' => true, // only get publicly accessible post types
    
        '_builtin' => false, // remove builtin post types
    
    );
    
    // generate post type list
    
    $post_types_for_rest = get_post_types($post_args, 'names');
    
    // add built-in 'post and page' post type
    
    $post_types_for_rest['post'] = 'post';
    
    $post_types_for_rest['page'] = 'page';
    
    foreach ($post_types_for_rest as $post_type_for_rest) {
    
        $args = array(
    
            'post_type' => $post_type_for_rest,
    
            'posts_per_page' => -1,
    
        );
    
        $the_query = new WP_Query($args);
    
        $posts = $the_query->posts;
    
        foreach ($posts as $post) {
    
            $post_id = $post->ID;
    
            $custom_field_keys = get_post_custom_keys($post_id);
    

    【讨论】:

      猜你喜欢
      • 2018-12-24
      • 1970-01-01
      • 2012-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-23
      • 1970-01-01
      相关资源
      最近更新 更多