Get all media from wp-json/wp/v2/media

Question

I’m struggling with a strange issue. I was pretty sure that I can use such a pattern to collect all media:

def get_all(url):
    all_results = []
    page = 1
    posts_url = "{0}?per_page={1}&page={2}&order=asc".format(url, 10, page)
    r = requests.get(posts_url)
    page_results = r.json()
    all_results.extend(page_results)
    while len(page_results) == 10:
        page += 1
        posts_url = "{0}?per_page={1}&page={2}&order=asc".format(url, 10, page)
        r = requests.get(posts_url)
        page_results = r.json()
        all_results.extend(page_results)

    return all_result

def get_all_media(url):
    return get_all("{0}wp/v2/media".format(url)

Unfortunately the:

wp-json/wp/v2/media?per_page=10&page=6&order=asc

call returns only 8 elements, however there are pages number:
7 – 8 elements,
8 – 1 element

Is it correct or my WordPress instance is malfunctioning?

0
SmallJoe 2 months 2022-08-11T03:19:30-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse