0

I've been trying to get the latest Page, not post, by author ID and display it on a page.

The reason it has to be a page and not a post is that the user will only be posting one page and a whole bunch of posts.

This also needs to be done dynamically and be based on the current logged in user.

I've got the following code:

<?php
$user_id = get_current_user_id();
echo $user_id;
$args = array(
    'post_type' => 'page',
    'post_status' => 'published',
    'posts_per_page' => 1,
    'author' => $user_id
);
$my_posts = get_posts( $args );

foreach ( $my_posts as $post ) : setup_postdata( $post ); ?>    
        <?php the_title(); ?>
<?php endforeach; ?>

All this does is return the title of the current page (testing with the_title(); will replace with the_content(); once it starts working). I've looked around for solutions but can't find any.

Would love some help with this.

1
  • 1
    Have you tried using WP_Query instead of get_posts?
    – Jared Cobb
    Commented Jul 11, 2014 at 14:21

2 Answers 2

0

You need to use WP_Query. get_posts will only get you post =)

<?php 
$user_id = get_current_user_id();
echo $user_id;
        $args=array(
            'post_type' => 'page',
            'post_status' => 'published',
            'posts_per_page' => 1,
            'author' => $user_id
        );                       
        $wp_query = new WP_Query($args);
        while ( have_posts() ) : the_post(); 
            the_title(); 
        endwhile; 
        ?>

Tested, and it is working

1
  • I've tried this but it breaks the connection to WordPress. An example is that when new users log in, they should be redirected to the page with this code on, unfortunately that page fails to load at all.
    – Gregory
    Commented Jul 15, 2014 at 8:07
0

I created a new custom post type rather than using a page. Should have thought of that sooner:

<?php
$user_id = get_current_user_id();
$args = array( 
    'post_type' => 'user-review', 
    'posts_per_page' => 1, 
    'author' => $user_id );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
    echo '<div style="width:885px;height:auto;">';
    the_content();
    echo '</div>';
endwhile; 
?>

Your Answer

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

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