• Resolved Ollie1700

    (@ollie1700)


    Hi everyone,

    I’m having this problem with the function wp_get_recent_posts, this function works fine on my local testing server however when I move it to my actual website this function appears to prevent the page from loading. I get no errors from the console, just any content after this method is called is not loaded.
    I am looking for a way to get the most recent post as an array so that I don’t have to edit masses of code. So I can access post elements like this: $post[‘post_title’], $post[‘post_author’] etc.

    Thanks.

Viewing 4 replies - 1 through 4 (of 4 total)
  • $args = array(
    	'posts_per_page' = 1
    );
    // The Query
    $the_query = new WP_Query( $args );
    // The Loop
    if ( $the_query->have_posts() ) {
    	while ( $the_query->have_posts() ) {
    		$the_query->the_post();
    		[ do stuff ]
    	}
    }
    /* Restore original Post Data */
    wp_reset_postdata();
    Thread Starter Ollie1700

    (@ollie1700)

    Thanks for your quick response.

    Is there any way I can get the post as an array though? At the moment, I have two cases: there is no specified post id via _GET so it loads the most recent one, there is a post id specified in _GET so it loads that one. The second case works fine as I get the post as an array using $post = get_post($_GET['postid'], ARRAY_A);
    Then I can display post stuff using $post[‘post_title/content/author/etc.’]
    If I have to use the method you suggested then I will have a lot of re-coding to do using the post as an object instead of an array.

    So is there any way to get the post as an array rather than an object?

    Thanks again.

    Posts are normally returned as objects rather than arrays. Why can’t you use $post->post_title etc?

    Thread Starter Ollie1700

    (@ollie1700)

    Because it would require a lot of re-coding as I have set it all up with arrays already. I guess I will have to do that though. Thanks for your help, I’ll use your method.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How can I get most recent post without using wp_get_recent_posts’ is closed to new replies.