How to display page title+link of page created by user

I want to check if user is logged in and if have created a page. If so, option 1 is shown. This all works fine.

But how to display page title+link of page created by that user between {page title with link to page}

See the code below I tested it with no results. As used in the code below, nothing special is showing, only option 1 text (which is good for the user have a page created and is logged in).

    <?php
        if ( is_user_logged_in() ) {
            global $wpdb;
        $user = wp_get_current_user();
        $where = get_posts_by_author_sql( 'page', true, $user->ID );
        $count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts $where" );

            if ( $count >= 1 ) { 

            // this part is added to display page title+link created by current user
            foreach ( $results as $result )
                printf( '<a href="%1$s">%2$s</a><br>', 
                get_permalink( $result->ID ),
                    esc_html( $result-post_title )
                ); ?>

//option 1
    <h2>Hey <?php echo $current_user->display_name ?>, check your page here: {page title with link to page} </h2>

    <?php } else { ?>
    //option 2
    <h2>Welcome <?php echo $current_user->display_name ?>, etc.. text with tags etc.</h2>

    <?php } } else { ?>
    //option 3
    <h2>text with tags etc.</h2>

    <?php } ?>

Any suggetions?

Answers 2

  • To list all pages with title and permalink from one user you need $wpdb->get_results(). The following code is based on this answer: How to count current user's pages?

    First, we move the counter into a separate helper function; we might need it later again:

    /**
     * Get all post IDs and titles of a type for a user.
     *
     * @param int $user_id
     * @param string $post_type 'page' (default), 'post', attachment, a custom post
     *                          type or 'any' (which excludes attachments)
     * @return array
     */
    function t5_user_pages( $user_id, $post_type = 'page' )
    {
        global $wpdb;
        $where = get_posts_by_author_sql( $post_type, true, $user_id );
        return $wpdb->get_results( "SELECT ID, post_title FROM $wpdb->posts $where" );
    }
    

    Now we use that function in our code:

    if ( is_user_logged_in() )
    {
        $user    = wp_get_current_user();
        $results = t5_user_pages( $user->ID );
        $count   = count( $results );
    
        if ( $count >= 1 )
        {
            print '<h2>Hello ' . esc_html( $user->display_name ) . '!</h2>
            <p>These are your pages:</p>
            <ul>';
    
            foreach ( $results as $result )
                printf( '<li><a href="%1$s">%2$s</a></li>',
                    get_permalink( $result->ID ),
                    esc_html( $result->post_title )
                );
    
            print '</ul>';
        } else {
            // user is logged in but hasn't written anything
        }
    }
    else
    {
        // user is not logged in
    }
    

  • Found it! check the code below:

    <?php
            if ( is_user_logged_in() ) {
            global $wpdb;
            $user = wp_get_current_user();
            $where = get_posts_by_author_sql( 'page', true, $user->ID );
            $count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts $where" );
    
                if ( $count >= 1 ) { ?>
    
        //option 1 if user is logged in and have created a page
        // added Toscho's code from answer 1 between second '<? php ?>'
        <h2>Hey <?php echo $current_user->display_name ?>, check your page here: <?php foreach ( $results as $result )
                        printf( '<a href="%1$s">%2$s</a>',
                        get_permalink( $result->ID ),
                        esc_html( $result->post_title )
                    ); ?></h2>
    
        <?php } else { ?>
    
        //option 2 if user is logged in, but have not yet submitted a page
        <h2>Welcome <?php echo $current_user->display_name ?>, etc.. text with tags and some form php code</h2>
    
        <?php } } else { ?>
    
        //option 3 is no user is not logged in
        <h2>text with tags etc and social login php code</h2>
    
        <?php } ?>
    

Related Questions