-2

Using WordPress REST API, I'm externally getting posts: https://example.com/wp-json/wp/v2/posts?orderby=date&order=desc&per_page=12

Body results:

[
    {
        "date": "2022-05-05T12:12:12",
        "link": "https://example.com/blog/132/",
        "title": {
            "rendered": "F: Title"
        }
    },
    {
        "date": "2022-04-04T12:12:12",
        "link": "https://example.com/blog/434/",
        "title": {
            "rendered": "E: Title"
        }
    },
    {
        "date": "2022-03-03T12:12:12",
        "link": "https://example.com/blog/324/",
        "title": {
            "rendered": "A: Title"
        }
    },
    etc...

]

I'd like to sort title by alphabetical order.

What would be the best solution?

I'm unable to use WP_Query since the posts are being pulled from an external WP API source.

5
  • stackoverflow.com/questions/43153737/… Commented May 21, 2022 at 10:34
  • @AmiraliAmirifar Thanks for this, unfortunately it didn't work for me Commented May 21, 2022 at 11:02
  • This is a json response. So it's an array of json objects. Where and in what language do you want to sort this? Is the client calling the api a php-client or is it some frontend js-client? Commented May 21, 2022 at 11:20
  • @Christoffer I'd like to sort this in PHP and the client calling the API is also PHP. The thing with WordPress Rest API, I can set order by using GET parameters. But I need to get the latest posts in DESC order AND by alphabetical order. This would have been super easy using WP_Query, but it's not possible when calling an API endpoint from an external source. Currently, I'm able to get the latest posts in DESC order in JSON. Now I'd like to sort the titles by alphabetical order using PHP and then foreach loop and print as HTML. Commented May 21, 2022 at 12:23
  • If you want multiple sorting rules, then write arrays of value on both sides of the spaceship operator like this Commented May 21, 2022 at 14:11

2 Answers 2

0

May be something like that :

<?php
function cmp($a, $b)
{
    if ($a['title']['rendered'] == $b['title']['rendered']) {
        return 0;
    }
    return ($a['title']['rendered'] < $b['title']['rendered']) ? -1 : 1;
}

$ar = json_decode($yourJson, TRUE);
usort($ar, "cmp");

...

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

3 Comments

This function worked fine until I passed the newly sorted array in a foreach loop. The data in each array somehow is not printing correctly.
It is not the answerer's fault if you have not clearly presented your scenario.
@svgta thank you for this. I discovered that when json_decode associative is set to true, I had to change from object operator to array. For instance, I changed this $post->title->rendered to $post['title']['rendered'] and all worked out!
0

use this function to sort any array

function array_sort($array, $on, $order = SORT_ASC)
{
    $new_array = array();
    $sortable_array = array();

    if (count($array) > 0) {
        foreach ($array as $k => $v) {
            if (is_array($v)) {
                foreach ($v as $k2 => $v2) {
                    if ($k2 == $on) {
                        $sortable_array[$k] = $v2;
                    }
                }
            } else {
                $sortable_array[$k] = $v;
            }
        }
        switch ($order) {
            case SORT_ASC:
                asort($sortable_array);
                break;
            case SORT_DESC:
                arsort($sortable_array);
                break;
        }


        foreach ($sortable_array as $k => $v) {
            $new_array[$k] = $array[$k];
        }
    }

    return $new_array;
}

1 Comment

Wow, whatever this unexplained script is doing, it definitely looks over-engineered /convoluted.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.