I am developing an android app for my WordPress blog. I have some bad trouble to detect and get post. So first I give u a vision from what I am into:
I want to get post from past, so I do this:
Get the biggest post id (in other words, the latest post ID), and then I get the minimum post ID in my Database in my app after that I can find the exact page number to fetch the post.
So I checked the result from my wp rest api plugin, an it seems like, post are change by plus 2 for each post, so I wrote my app based on this, but, now I look to the first post in my blog and Literally there is no order.
For some of the is plus one, for some other plus 3 and ... . so, the main question is, whats is the order of Change in post ID? Is there a way to set post Id, by hand?(an easy way) there should be an ordered variable, there is a tone of app out there, which can save post in app Database.
And sorry for my language.
Edit: add code
public void GetPostFromNet_LoadMore_firstStep() {
ServiceCall serviceCall = new ServiceCall("mysiteURL");
MyApi myApi = serviceCall.s();
Call<JsonElement> call2 = myApi.loadQuestions("/wp-json/posts?fields=ID,title,type,comment_status,modified_gmt,featured_image&filter[posts_per_page]=1&page=1");
call2.enqueue(new Callback<JsonElement>() {
@Override
public void onResponse(Response<JsonElement> response, Retrofit retrofit) {
try {
LoadMore_DoFirstStep(response);
} catch (Exception ex) {
Toast.makeText(MainActivity.this, ex.toString() + "__call2_somthing happend" + ex.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
ex.printStackTrace();
}
}
@Override
public void onFailure(Throwable t) {
t.printStackTrace();
Toast.makeText(getApplicationContext(), "onFailure", Toast.LENGTH_SHORT).show();
}
});
}
public void LoadMore_DoFirstStep(Response<JsonElement> response) throws Exception {
ArrayList<obj_sitePosts> postItems = new ArrayList<>();
JsonArray jsonArray = response.body().getAsJsonArray();
if (!isNull(jsonArray) && jsonArray.size() > 0) {
postItems = convertJson_getPosts(jsonArray);
if (!isNull(postItems) && postItems.size() > 0) {
long latest_ID = postItems.get(0).getID();
db_sitePosts db_newPost = new db_sitePosts(getApplicationContext());
long smallest_ID = db_newPost.getAllPost_OrderBy().get(db_newPost.getPostCount() - 1).getID();
long diffID = Math.abs(latest_ID - smallest_ID);
if (diffID > 0) {
int resualt = 0, quotient = 0, remaning = 0;
resualt = (int) Math.floor(diffID / 2);
quotient = (int) Math.ceil(resualt / POST_LOAD_PER_REQUEST);
remaning = resualt % POST_LOAD_PER_REQUEST;
if (quotient > 0) {
if (remaning == 0) {
//call function usign page=quotient+1; andd POST_LOAD_PER_REQUEST
LoadMore_DoSecendStep(quotient + 1, POST_LOAD_PER_REQUEST);
}
if (remaning > 0) {
LoadMore_DoSecendStep(quotient, POST_LOAD_PER_REQUEST);
}
}
long page = diffID / POST_LOAD_PER_REQUEST;
} else {
//it mean site have just one POST!!!
Toast.makeText(getApplicationContext(), "no new Post!!!", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getApplicationContext(), "no post has been sent Yet!!!", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getApplicationContext(), "no post has been sent Yet!!!", Toast.LENGTH_SHORT).show();
}
}
GetPostFromNet_LoadMore_firstStep: to get the biggest POST ID.
LoadMore_DoFirstStep: to find the number of post between minimum post id exist in db and whitch came from GetPostFromNet_LoadMore_firstStep.
LoadMore_DoSecendStep: to get the right page
WP_Query
withpost_type
anddate_query