Wordpress get the data and push into array

I want to get the data from WordPress DB, pushed them into array and past the data to JS.

Got it all working except pushing the data into array in foreach ($posts as $post).

Full method:

        $array = array(); 
        $category_name = "locations";
        $args = array(
            'numberposts' => -1,
            'category_name' => $category_name
        );

        $posts = get_posts($args);      
        foreach ($posts as $post){
            array_push( $array, array(

                "title" => the_title($post->ID),
                "cnt" => the_content($post->ID),
                "id" => the_ID($post->ID),
                "link" => the_permalink($post->ID)

            )); 
        }

When I do print_r($array) i get a following mix:

Post 39http://localhost:8080/testing/post-3/Post 27http://localhost:8080/testing/post-2/Post 15http://localhost:8080/testing/post-1/Array
(
    [0] => Array
        (
            [title] => 
            [cnt] => 
            [id] => 
            [link] => 
        )

    [1] => Array
        (
            [title] => 
            [cnt] => 
            [id] => 
            [link] => 
        )

    [2] => Array
        (
            [title] => 
            [cnt] => 
            [id] => 
            [link] => 
        )

)

What is going on? Why is the data not placed correctly into the array? Any suggestions much appreciated.

Answers


Those template tags need to have their echo variable set to false to work in that context. They also don't take post ID as a variable, they assume the current post if postdata is setup. Try:

the_title('','',false)

https://codex.wordpress.org/Function_Reference/the_title


-~ -~ -~ -~ -~ -~ -~ -~ CLARIFICATION EDIT -~ -~ -~ -~ -~ -~ -~ -~

The tidiest solution to this problem was actually to directly address the wp $post object. So for the title string $post->post_title and for the content $post->post_content

The fields available in the $post object are listed here:

http://codex.wordpress.org/Function_Reference/get_post#Return


These functions (the_title, the_content, etc) do not return values, they echo them to output. They also don't take an ID argument. You need to use get_the_title, get_the_content, etc., the versions that return a string.


Need Your Help

why is my SetBookTitle not storing the strings

c++ arrays string class variables

Strangely when I enter a title for the member to store it doesn't store it what is wrong with it. I dont know what the problem is i have tried a lot of things. I know the median variable works fine...

After first request HttpClientHandler is disposed

c# windows-phone httpclient

I want to send two request from my ViewModel (first - GET and then - POST) using HttpClient. GET request completes without any error. But if then I send POST request I got exception:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.