I am working on an app that uses the Joomla API to query articles in a specific category.
Example: api/index.php/v1/content/articles?filter[category]=2
However, this returns all articles regardless of their published state. Which in turn is causing other issues in the way I'm processing the returned json data.
That aside:
I used the filters option in the URL to only return those articles that are either published [1], or unpublished [0]
api/index.php/v1/content/articles?filter[category]=2&filter[state]=1&filter[state]=0
However, this does not work.
Regardless of the order I place the filters, only the last filter in the string is being returned.
Example 1: api/index.php/v1/content/articles?filter[category]=2&filter[state]=1&filter[state]=0
Returns only a list of unpublished articles
Example 2: api/index.php/v1/content/articles?filter[category]=2&filter[state]=0&filter[state]=1
Returns only a list of published articles
I also tried concatenated versions, which returned the json results, but again only the final part.
api/index.php/v1/content/articles?filter[category]=2&filter[state]=1,0
api/index.php/v1/content/articles?filter[category]=2&filter[state]=[0,1]
ETC...
It would seem from the online Postman documentation that Joomla can accept multiple filters in the same URL
For example when querying users:
Example: /api/index.php/v1/users?filter[groupid]=8&filter[state]=0&filter[registrationDateEnd]=2099-12-31T20%3A36%3A01Z' \
But, in this case for article retrieval maybe not, or there is a bug.
The API to return all published and unpublished articles in the selected category
Only articles linked to the last filter in the API url are being returned.
Joomla 5.1.4 php 8.2
Labels |
Removed:
?
|
Labels |
Added:
No Code Attached Yet
|
@lushdomain Try the following:
api/index.php/v1/content/articles?filter[category]=2&filter[state][]=0&filter[state][]=1
.
Mind the new array element []
after the filter[state]
.
Does that work?
@richard67 It works, but not as I'd expect. I did look at this option before, but couldn't get it to work either at the time. However, with pared down articles for testing, ie 2 (1 published and 1 unpublished) it does work to return both articles.
However, there is a new twist now.
/api/index.php/v1/content/articles?filter[category]=2&filter[state][]=0 returns unpublished as well as published.
/api/index.php/v1/content/articles?filter[category]=2&filter[state][]=1 returns published as well as unpublished.
/api/index.php/v1/content/articles?filter[category]=2&filter[state][]=0&filter[state][]=1 returns unpublished and published.
The json response seems to place the unpublished article first regardless of how the url is structured, I don't know if this matters or is a normal response.
However, there is a new twist now.
/api/index.php/v1/content/articles?filter[category]=2&filter[state][]=0 returns unpublished as well as published.
/api/index.php/v1/content/articles?filter[category]=2&filter[state][]=1 returns published as well as unpublished./api/index.php/v1/content/articles?filter[category]=2&filter[state][]=0&filter[state][]=1 returns unpublished and published.
@lushdomain You have to use the []
only when specifying more than one value so the filter[state]
shall be an array. When only filtering for one value, do not use []
. If the following works for you then it works as desired:
/api/index.php/v1/content/articles?filter[category]=2&filter[state]=0
returns unpublished only.
/api/index.php/v1/content/articles?filter[category]=2&filter[state]=1
returns published only.
/api/index.php/v1/content/articles?filter[category]=2&filter[state][]=0&filter[state][]=1
returns unpublished and published but not archived and not trashed.
In general use somearray[somekey]=value
to assign one value to that somearray[somekey]
, so the somearray[somekey]
is a scalar, and use somearray[somekey][]=value1&somearray[somekey][]=value2&somearray[somekey][]=value3
to assign mutliple values so the somearray[somekey]
is an array.
Example: api/index.php/v1/content/articles?filter[category]=2
However, this returns all articles regardless of their published state. Which in turn is causing other issues in the way I'm processing the returned json data.
Are you sure? For me, it returns published and unpublished articles only without having to specify state filter.
Labels |
Added:
Information Required
|
i can confirm there is no need of filter cause the endpoint returns both published and unpublished
Status | New | ⇒ | Closed |
Closed_Date | 0000-00-00 00:00:00 | ⇒ | 2024-10-17 11:49:40 |
Closed_By | ⇒ | Quy |
Closing as non-reproducible.
When using filters I believe that the filters are published and category_id
not at my pc to check