wp-net / WordPressPCL

This is a portable library for consuimg the WordPress REST-API in (almost) any C# application
MIT License
335 stars 131 forks source link

Media.Query not return all Media #297

Open moskyow opened 2 years ago

moskyow commented 2 years ago

Hi, with vb.net With the following code the query return only 10 Media of the current year, also if i set the value of year "before" on 2012 (cboAnno.text) The number media I return with method GetAll is more than 400

        Dim strData As String = cboAnno.Text & "-01-01"
        Dim DataStart As DateTime = DateTime.ParseExact(strData, "yyyy-MM-dd", Nothing)
        strData = Year(Now) & "-12-31"
        Dim DataEnd As DateTime = DateTime.ParseExact(strData, "yyyy-MM-dd", Nothing)
        Dim queryBuilder As New Utility.MediaQueryBuilder
        queryBuilder.After = DataStart
        queryBuilder.Before = DataEnd
        Dim medias = Await WPClient.Media.Query(queryBuilder)

What is the problem ?

navjot50 commented 2 years ago

MediaQueryBuilder by default paginates your result. The default page is 1 and default results per page are 10. Have a look at Page and PerPage property of MediaQueryBuilder.

You can loop through the pages to get all the required media. Alternatively, you can also use the GetAll method that you mentioned.

moskyow commented 2 years ago

MediaQueryBuilder by default paginates your result. The default page is 1 and default results per page are 10. Have a look at Page and PerPage property of MediaQueryBuilder.

You can loop through the pages to get all the required media. Alternatively, you can also use the GetAll method that you mentioned.

Thank you, I see that also PostsQueryBuilder have the same default 10 posts per page. How I can find the collection of Pages for media and posts returned by the query to loop ?

navjot50 commented 2 years ago

You will have to add a for loop in your code and then increment the page number for the query builder for each iteration of your for loop and then call the Query method. This will then issue a http request for each iteration and give back the results.

Also, you can set the PerPage to 100 (this is the max value supported by Wordpress Rest api). This way you will get 100 Media/Posts for each iteration of for loop.

moskyow commented 2 years ago

You will have to add a for loop in your code and then increment the page number for the query builder for each iteration of your for loop and then call the Query method. This will then issue a http request for each iteration and give back the results.

Also, you can set the PerPage to 100 (this is the max value supported by Wordpress Rest api). This way you will get 100 Media/Posts for each iteration of for loop.

Thank you. Ok, I can increment the Page and repeat the request for each loop, but can I know the maximum number of pages to loop ? If I must have the total number of media by GetAll method, the query is useless.

moskyow commented 2 years ago

Ok, resolved. I check if the page that I loop have or not the number of Media equals at PerPage number. It seems that GetAll returns all media_type, but the Query only image file. It's a bug or a default setting ?

navjot50 commented 2 years ago

Yes, by default it only queries images because the MediaQueryType is set to Image by default. @ThomasPe is it expected for media query builder to give only images?

ThomasPe commented 2 years ago

I don't see where it would be set to images by default, is this a fallback of the API? I would be fine with the Query Builder returning all types by default.

navjot50 commented 2 years ago

The MediaType property in the MediaQueryBuilder is of MediaQueryType enum type. Enums by default are set to the first item of the enum which in case of MediaQueryType is Image. So, if a MediaQueryBuilder object is created without setting the MediaType property, then this property is by default set to Image.

We also have a test Media_Query in Media_Tests class which confirms that the url contains the media_type=image substring causing only images to return.

moskyow commented 2 years ago

I don't see where it would be set to images by default, is this a fallback of the API? I would be fine with the Query Builder returning all types by default.

On my code I use MediaQueryBuilder, but it return only images

ThomasPe commented 2 years ago

I just published a new 2.0.0-beta.2 for release 2 that includes a bugfix from @navjot50 (Thanks!) which allows you to pass "All" as an enum to the query builder. Let me know if this helped!