• Hi,

    I tried to use the “List Posts” method from the REST API v2 (List Post).
    I need to get the list post by for two categories at a time, for example:

    The request https://www.attivitiamo.it/wp-json/wp/v2/posts?_embed=&per_page=10&page=1&categories=16,3

    The version of my wp is 5.4.2

    return a different result of wp query by category_and = [16, 3] launched in pre_get_posts hook.

    I read from blogs where it invite to use categories=16+3 or ategories[]=…. or with filter param but all statements non work and it not specified in the official doc.

    Can you help me please?

    Regards
    Luca

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    “categories” arg uses OR logic for listed IDs. I’m unaware of a query arg in the API that would give you AND logic. Judging from the lack of any other response to this topic, apparently no one else does either. To get OR logic, you’d need to alter the query through “pre_get_posts”. The categories arg ends up as a “tax_query” arg, so you merely need to set the “operator” element of the inner array to be “AND”. By default there is no such element, so the logic defaults to “IN” (which is a logical OR operation)

    To avoid altering other queries, check various query vars to ensure you only alter the intended query. Start by verifying REST_REQUEST constant has been defined as true.

Viewing 1 replies (of 1 total)
  • The topic ‘REST API /posts by categories’ is closed to new replies.