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" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener">%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:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener">%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
}
Method 2
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" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener">%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 } ?>
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0