-1

I have an app for wordpress blog site. this app getting wordpress articles with php api call.

I am using this code for get article list.

function api_posts()
{
    $args = [
        'numberposts' => 99999,
        'post_type' => 'post',
    ];

    $posts = get_posts($args);

    $data = [];
    $i = 0;

    foreach ($posts as $post) {
        $data[$i]['id'] = $post->ID;
        $data[$i]['title'] = $post->post_title;
        $data[$i]['excerpt'] = $post->post_excerpt;
        $data[$i]['content'] = $post->post_content;
        $data[$i]['slug'] = $post->post_name;
        $data[$i]['thumbnailImage'] = get_the_post_thumbnail_url($post->ID, 'thumbnail');
        $data[$i]['mediumImage'] = get_the_post_thumbnail_url($post->ID, 'medium');
        $data[$i]['largeImage'] = get_the_post_thumbnail_url($post->ID, 'large');
        $data[$i]['date'] = $post->post_date;;
        $data[$i]['post_url'] = get_permalink($post->ID);
        $i++;
    }

    return $data;
}

I am getting ID, post_excerpt, post_content and others. So I am getting 10 information about one article. But I want to get more than 10 information about one artice. But I don' know the keys to get informations. For example I want to get article category. How can I do this. Where can I learn keys like post_title, post_excerpt. I know developers.wordpress but I don't understand it.

This is my custom wordpress api result https://meshcurrent.online/wp/wp-json/api/v1/posts/

2 Answers 2

2

EDIT

Here's 1 more re-write but with a query as for some strange reason get_post_meta was not giving all the meta data as it used to before:

function api_posts(){
    global $wpdb;

    $posts_with_meta = $wpdb->get_results( 
        "SELECT *
        FROM $wpdb->posts INNER JOIN $wpdb->postmeta
        on $wpdb->posts.`ID` = $wpdb->postmeta.`post_id` where $wpdb->posts.`post_type` = 'post' 
        "
    );

    return $posts_with_meta;
}

OLD ANSWER

Here's how you can get all the meta keys and their values of all the posts:

function api_posts()
{
    $args = [
        'numberposts' => 99999,
        'post_type' => 'post',
    ];

    $posts = get_posts($args);

    $data = [];
    $i = 0;

    foreach ($posts as $post) {
        $allPostMeta = get_post_meta($post->ID);

        foreach($allPostMeta as $key => $value) {
            $data[$i][$key] = $value[0];
        }
        $i++;
    }

    return $data;
}

I've just done a re-write of your code to get you all the keys with their data. I can't test or replicate it as it's specific to your environment but I'm sure it'll work just fine for you.

Some keys might differ for your blog app and the website WordPress it's called post_title but your app reads it as the title. You'll need to work around that. But this will pretty much give you a list of all meta keys applicable to a post and you can then play around with your code and add only those keys which you need.

Once you know the fields you want in the API, You must also checkout WP_QUERY as that is more powerful and has a fields filter which can give you only those fields that you need for the app and not push all the data. Thus, saving API response time.

I hope this will help you.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your help but I guess it's not working. You can go the link again to see result.
You were right, it was not returning all data. I did a re-write again and it's giving all data.
Thanks again. You are in correct way but this is not the best answer. Because there are duplicated items in result as article. I have 5 articles on my website. But there are items in result more than 5. And some keys are not exist(for example category). But even this code is so helpful for me.
I agree, that this is not the best answer and we should be keeping raw queries as the very last resort. I personally don't like the raw queries. I'll figure out a better way for this.
0

I don't think there is any function available to return all data. You'll have to collect all the data manually.

If you're interested in getting categories and tags then you'll have to use wp_get_post_terms function to get the terms objects array and then loop through values and get the names/slugs/term_id etc.

If you're interested in metadata, then you'll have to use get_post_meta function to get meta data using meta keys.

1 Comment

He knows the methods, the problem is, he don't know the meta_keys. get_post_meta with just post id was supposed to give whole set of meta data but it isn't doing that for some reason.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.