使用 Url Wordpress 获取查询? wp 休息 API

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

我有一个帖子类型属性。属性有名称(标题)图像(特色图像)和位置(Acf 字段)。所以我想提取确切的属性。从前端我有一个搜索表单,我在其中使用位置搜索属性。所以我可以使用像

domain/wp-json/wp/v2/properties?acf.address_map.address="formsearchlocation
这样的URL获得确切的属性,这样它只返回与我搜索的位置相同的属性使用wprestapi和前端进行反应,所以如果有人可以帮助我回复!!

(我不想先获取所有位置然后过滤掉特定位置

try:- domain/wp-json/wp/v2/properties?acf.address_map.address="new-jersey"
Expectation :- title : Broadway Home
               Image : domian/uploads/media/broadway.webp
               Location : new-jersey United States

Output :- [
{
      "id": 11371,
       "title": "Business Address"
       "acf" : {
             "address_map" : { 
                     "location" : "West 40th Street, New York, NY 10018",
             }
        }
},{
      "id": 11352,
       "title": "Prime Address"
       "acf" : {
             "address_map" : { 
                     "location" : "West 40th Street, London",
             }
        }
},
{
      "id": 11371,
       "title": "Business Address 2"
       "acf" : {
             "address_map" : { 
                     "location" : "East 40th Street, australia",
             }
        }
},{
      "id": 11352,
       "title": "Prime Address 2"
      "acf" : {
             "address_map" : { 
                     "location" :  "new-jersey United States",
             }
        }
},
]
wordpress post get frontend wordpress-rest-api
1个回答
0
投票

为了使用 REST API 按特定位置过滤属性,我们需要创建自定义 REST API 或端点来为您过滤数据。

下面是在 WordPress 中创建

REST API endpoint
的代码,您可以将其添加到主题的
functions.php
文件中。

function register_properties_route() {
    register_rest_route('custom/v1', '/properties', array(
        'methods'  => 'GET',
        'callback' => 'get_properties_by_location',
        'args'     => array(
            'location' => array(
                'required'          => true,
                'validate_callback' => function($param, $request, $key) {
                    return is_string($param);
                }
            ),
        ),
    ));
}

add_action( 'rest_api_init', 'register_properties_route' );

function get_properties_by_location( $request ) {
    $location = sanitize_text_field( $request['location'] );
    $args     = array(
        'post_type'  => 'properties',
        'meta_query' => array(
            array(
                'key'     => 'address_map_location', // Replace with your actual ACF field key
                'value'   => $location,
                'compare' => 'LIKE'
            )
        )
    );

    $query = new WP_Query( $args );
    $posts = array();

    if ( $query->have_posts() ) {
        while ($query->have_posts()) {
            $query->the_post();
            $posts[] = array(
                'id'    => get_the_ID(),
                'title' => get_the_title(),
                'acf'   => get_fields(get_the_ID())
            );
        }
        wp_reset_postdata();
    }

    return new WP_REST_Response( $posts, 200 );
}

您可以使用此端点根据您的需要过滤属性。这是您可以在

ReactJS
中使用的示例函数。

const [properties, setProperties] = useState([]);
const handleSearch = async () => {
try {
   const response = await axios.get(`/wp-json/custom/v1/properties`, {
        params: {
          location: location,
        },
      });
      setProperties( response.data );
    } catch (error) {
      console.error( 'Error in fetching properties:', error );
    }
  };

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.