【问题标题】:Add post with custom field value to WP Rest API将具有自定义字段值的帖子添加到 WP Rest API
【发布时间】:2021-01-01 07:19:59
【问题描述】:

我在 WordPress 中为博客文章创建了一个自定义字段。这是“是”或“否”的简单单选按钮。我想知道是否有一种方法可以向 WP REST API 添加一个数组字段,该字段将包含所有选择“是”的帖子。如有必要,我可以提供更多信息。我无法找到真正专门解决这个问题的资源,但希望有人能提供任何输入或资源。

【问题讨论】:

    标签: php wordpress advanced-custom-fields wordpress-rest-api


    【解决方案1】:

    将此添加到functions.php 文件中:

    function endpoint_acf() {
        $args = array(
          'meta_key'   => 'custom-fied-name', 
          'meta_value' => 'custom-field-value'
        );
        $the_query = new WP_Query( $args );
        return $the_query;
    }
    
    add_action('rest_api_init', function() {
        register_rest_route('wp/v2', 'endpoint-name', array(
            'methods' => array('GET', 'POST'),
            'callback'    => function() {
                    return endpoint_acf();      
                },
        ));
    });
    

    【讨论】:

      猜你喜欢
      • 2018-02-05
      • 1970-01-01
      • 2018-06-24
      • 1970-01-01
      • 2015-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多