0

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

  • Do you have a code example? WordPress DOES increment post ids by one, but the posts table stores data for different types of posts (and menus and attachments), so you are better off using WP_Query with post_type and date_query – czerspalace Mar 3 '16 at 2:32
  • It's difficult to understand what you've written. I made some improvements such as paragraphs and correct capital letters in places which may help – Tom J Nowell Mar 3 '16 at 2:36
  • i am so sorry and it is not OK to say , but i am not well in WordPress. it should go and do some search. @czerspalace – mil Mar 3 '16 at 5:34
  • @TomJNowell oh, tank u my brother, it was nice of you. – mil Mar 3 '16 at 5:35
0

Post ID is not an external ID of the post but an internal one and you should not expect any consistency in it. Since almost everything is stored at the post table the value of ID depends not only on the number of "true" posts but also the number of revisions, images and any other CPT.

On top of that there are post that are not publicly available like drafts.

You should just use the offset and the number of results that you want as parameters to the query.

  • so i can't use post ID, and the only exist way is to get all the post some how, right? can i add a function like this: GiveMePostCountFrom(long ID), this function get one ID and return the number of post, witch came after that, for example it take 9 and say, there is 120 new post came after you with bigger ID? can i have some link(any link would be helpful) for adding function into wp rest api? – mil Mar 3 '16 at 5:36
  • you should probably use dates instead of IDs if you want chronological order – Mark Kaplun Mar 3 '16 at 5:58
  • it is good, and i think (as i mention) create a function witch take an id and return the number of post came after that. it is something which i need to implement in WordPress rest api. and it is some kinda odd, i think load a page start from a ID(a post) it is very common usage and there is no function for this in wp rest api. – mil Mar 3 '16 at 11:27
  • it is not a common usage at all, I actually never seen anyone do such a thing and I have been around wordpress for a long time – Mark Kaplun Mar 3 '16 at 12:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.