最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Wordpress + ACF | REST API Query the Results by the Initial Character - Stack Overflow

programmeradmin0浏览0评论

I’m developing a custom data directory using WordPress and Custom Form Fields, and I’m fetching the data through the WordPress REST API to display it in a Next.js web application.

I need to filter the results based on the starting character of a custom field. However, I couldn't find a built-in option to filter results by the starting character when querying the WordPress REST API for Custom Form Fields.

Here’s my server data fetching server action.

export async function fetchDrugs(filter: {
  search?: string;
  character?: string;
  page?: number;
}) {
  try {
    console.log("Character" + filter.character);
    const params: any = {
      per_page: 8,
      page: filter.page || 1,
      orderby: "title",
      order: "asc",
      _fields: "id,title,acf", // Optimize response
    };

    if (filter.search) {
      params.search = filter.search;
    }

    if (filter.character) {
      params.meta_query = JSON.stringify([
        {
          key: "drug",
          value: `${filter.character}%`, 
          compare: "LIKE",
        },
      ]);
    }

    const res = await axios.get(`${backendURL}/wp-json/wp/v2/drugs`, {
      params,
    });

    return {
      drugs: res.data,
      totalPages: res.headers["x-wp-totalpages"],
    };
  } catch (error: any) {
    console.error("Error fetching drugs:", error);
    throw error;
  }
}


 URL QUERY PARAMS: http://localhost:3000/drug?character=Z

How can I modify my query to filter the custom field data by the starting character of the field value using the WordPress REST API?

发布评论

评论列表(0)

  1. 暂无评论