Quantcast
Channel: Active questions tagged rest - Stack Overflow
Viewing all articles
Browse latest Browse all 3663

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

$
0
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

enter image description here

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?


Viewing all articles
Browse latest Browse all 3663

Trending Articles