Filter posts by meta query

Hi,

I have a relation acf field to link a user to a post, it works great on wp, I can link a user on post edit,i’ve added those fields in the rest api,
Now i want to fetch those posts and filter them by this acf field, so when i do https://mywebsitet/wp-json/wp/v2/posts/?meta_key=nom_entreprise&meta_value=14 in Postman it returns my filtered posts. Meta key is the name of the acf field and meta value is the user ID

The weird part is that when i do

  useEffect(() => {
    actions.source.fetch("/?meta_key=nom_enteprise");
  }, []);

  const postMeta = state.source.get("/?meta_key=nom_enteprise");

frontity return the 20 posts from homepage, not my filtred posts

what am i missing ? thanks

I’ve managed to get it working, here is my handler

const EntrepriseHandler = {
  name: "EntrepriseMeta",
  priority: 5,
  pattern: "posts",

  func: async ({ route, params, state, libraries }) => {
    const { api } = libraries.source;
    console.log("entreprise", route);
    // 1. fetch the data you want from the endpoint page
    const response = await api.get({
      endpoint: "posts",
      params: {
        meta_key: "nom_enteprise"
      },
    });

    // 2. get an array with each item in json format
    const items = await response.json();

    // 3. add data to source
    const currentPageData = state.source.data[route];

    Object.assign(currentPageData, {
      items,
    });
  },
};

export default EntrepriseHandler;

and then call the handler wherever you want to get data

 useEffect(() => {
    actions.source.fetch("posts");
  }, []);

  const postMeta = state.source.get("posts");
1 Like

Also I forgot but i used this on the wordpress side