Krishna
Krishna

Reputation: 951

How to get articles by category in Strapi CMS

http://localhost:1337/posts?_where[categories]=<catId>_sort=createdAt:DESC&_start=0&_limit=3

Used the above API pattern to filter posts by categories but it didn't work. Does anyone know how to get this work. Posts has and belongs to many Categories

Upvotes: 1

Views: 4906

Answers (6)

krcky
krcky

Reputation: 101

As an addition to @Blue_Abe1's answer, I would like to share how I solved this using qs, which is Strapi's way of creating complex queries:

import qs from 'qs';

const queryParams = {
  filters: {
    categories: {
      name: {
        $eq: 'food'
      }
    }
  }
};

const queryString = qs.stringify(queryParams);
const articles = await fetch(`http://localhost:1337/articles?${queryString}`)

Upvotes: 0

Blue_Abe1
Blue_Abe1

Reputation: 106

It's not a big problem. Just go strapi cms panel choose articles in content manager and use filter button to filtering articles by category. When strapi do this check address bar in browser and copy last part with filter to your request. that's all.

enter image description here

Upvotes: 8

Rayhan
Rayhan

Reputation: 1

1.create Enumeration name = type 2.create your items in Enumeration

** [filters][enumeraton name]
** ${type} = your props type
http://localhost:1337//products?populate=*&[filters][type][$eq]=${type}
 

Upvotes: 0

Nsamba Isaac
Nsamba Isaac

Reputation: 495

with Strapi v4 i was able to filter my products by category like so:

http://localhost:1337/api/products?filters[$and][0][category][category][$eq]={categoryName}

And this with Images included

http://localhost:1337/api/products?filters[$and][0][category][category][$eq]={categoryName}&populate=*

Upvotes: 4

Daniel Murphy
Daniel Murphy

Reputation: 441

Version 4.0.3 Try this: http://localhost:1337/api/posts?filters[category][catID][$eq]=2

This worked for me.

V 4.0 Deep Filtering Docs: https://docs.strapi.io/developer-docs/latest/developer-resources/database-apis-reference/rest/filtering-locale-publication.html#deep-filtering

Upvotes: 1

Krishna
Krishna

Reputation: 951

http://localhost:1337/posts?_where[categories.name]=ACL&_sort=createdAt:DESC&_start=0&_limit=3

That is how it is done. We can replace name with id or any other properties

Upvotes: -1

Related Questions