Can't get all author using WP_User_Query - wordpress

I am trying to show all authors as well as the pagination of the result. So using bellow code it's not returning any users.
// Pagination vars
$current_page = get_query_var('paged') ? (int) get_query_var('paged') : 1;
$users_per_page = 12; // RAISE THIS AFTER TESTING ;)
$args = array(
'number' => $users_per_page,
'paged' => $current_page,
'role' => 'Author',
);
$users = new WP_User_Query( $args );
$total_users = $users->get_total(); // How many users we have in total (beyond the current page)
$num_pages = ceil($total_users / $users_per_page); // How many pages of users we will need
if ( $users->get_results() ) {
foreach( $users->get_results() as $user ) {
// $firstname = $user->first_name;
// $lastname = $user->last_name;
// $email = $user->user_email;
$author_thumb_url = get_avatar_url( $user->ID );
echo '<div class="col-md-6">';
echo '<div class="author-wrapper">';
echo '<div class="author-thumb">';
echo '<img src="'.$author_thumb_url.'">';
echo '</div>';
echo '<div class="author-description">';
echo '<h2>'. $user->first_name . ' ' . $user->last_name . '</h2>';
echo '<p>'. get_the_author_meta( 'description', $user->ID ) . '</p>';
echo '</div>';
echo '</div>';
echo '</div>';
}
}
?>
In args I have also used 'role' => 'author' and 'role__in' => 'author' but no result is showing.

Your loop might be incorrect. This is working for me. I also wrapper your columns in a row and container.
Try
// Pagination vars
$current_page = get_query_var('paged') ? (int) get_query_var('paged') : 1;
$users_per_page = 12; // RAISE THIS AFTER TESTING ;)
$args = array(
'number' => $users_per_page,
'paged' => $current_page,
'role' => 'author',
);
$users = new WP_User_Query( $args );
$total_users = $users->get_total(); // How many users we have in total (beyond the current page)
$num_pages = ceil($total_users / $users_per_page); // How many pages of users we will need
if ( $users->results ) {
echo '<div class="container"><div class="row">';
foreach( $users->results as $user ) {
// $firstname = $user->first_name;
// $lastname = $user->last_name;
// $email = $user->user_email;
$author_thumb_url = get_avatar_url( $user->ID );
echo '<div class="col-md-6">';
echo '<div class="author-wrapper">';
echo '<div class="author-thumb">';
echo '<img src="'.$author_thumb_url.'">';
echo '</div>';
echo '<div class="author-description">';
echo '<h2>'. $user->first_name . ' ' . $user->last_name . '</h2>';
echo '<p>'. get_the_author_meta( 'description', $user->ID ) . '</p>';
echo '</div>';
echo '</div>';
echo '</div>';
}
echo '</div></div>';
}
``

Related

create a pagination with shortcodes

on my Wordpress site, I created CPT shortcodes, I want there to be 6 posts per page. But when I have more posts, I want the most anician to be visible and to set up a pagination.
how can I add that?
Here is my current code:
function diwp_create_shortcode_newprojects_post_type(){
$args = array(
'post_type' => 'projets',
'posts_per_page' => '6',
'category_name' => 'neufs',
'publish_status' => 'published',
);
$query = new WP_Query($args);
if($query->have_posts()) :
while($query->have_posts()) :
$query->the_post() ;
$content = wpautop( get_the_content() );
/* $result .= '<div class="post-item">';
$result .= '<div class="post-poster">' . get_the_post_thumbnail() . '</div>';
$result .= '<div class="post-name">' . get_the_title() . '</div>';
$result .= '<div class="post-desc">' . get_the_content() . '</div>';
$result .= '</div>'; */
$result .= '<div class="block-actu-list"> <div class="img-actu">' . get_the_post_thumbnail( $post->ID, 'large') . '</div> <div class="legend">' . get_the_title() . '</div></div>';
endwhile;
wp_reset_postdata();
endif;
return $result;
}
add_shortcode( 'newprojects-listed', 'diwp_create_shortcode_newprojects_post_type' );
----------------- Edit -----------------
With #Saqib Amin 's answer I'm trying this, is this correct ?
// CPT shortcode HomePage
function diwp_create_shortcode_project_post_type(){
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'projets',
'posts_per_page' => '10',
'paged' => $paged,
'category_name' => '',
'publish_status' => 'published',
);
$query = new WP_Query($args);
if($query->have_posts()) :
while($query->have_posts()) :
$query->the_post() ;
$content = wpautop( get_the_content() );
/* $result .= '<div class="post-item">';
$result .= '<div class="post-poster">' . get_the_post_thumbnail() . '</div>';
$result .= '<div class="post-name">' . get_the_title() . '</div>';
$result .= '<div class="post-desc">' . get_the_content() . '</div>';
$result .= '</div>'; */
$result .= '<div class="block-actu-list"> <div class="img-actu">' . get_the_post_thumbnail( $post->ID, 'large') . '</div> <div class="legend">' . get_the_title() . '</div></div>';
endwhile;
wp_reset_postdata();
endif;
return $result;
}
add_shortcode( 'project-listed', 'diwp_create_shortcode_project_post_type' );

How to get subcategories in category.php NOT posts

Is there any way to get only subcategories in category.php file instead of posts?
I know that WordPress paginates category.php by doing its own query and that I should not create a new query, but I need to show only the child categories in some categories instead of posts.
And, because I have many, I also need it to be paginated...
Can you please try below code:
$args = array('child_of' => get_query_var('cat') );
$categories = get_categories( $args );
$numOfItems = 2; // Set no of category per page
$page = isset( $_GET['cpage'] ) ? abs( (int) $_GET['cpage'] ) : 1;
$to = $page * $numOfItems;
$current = $to - $numOfItems;
$total = sizeof( $categories );
echo '<ul class="content">';
for( $i=$current; $i<$to; ++$i ) {
$category = $categories[$i];
if( $category->name ) {
echo '<li>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a></li>';
}
}
echo '</ul>';
unset( $category );
/* For pagination */
echo paginate_links( array(
'base' => add_query_arg( 'cpage', '%#%' ),
'format' => '',
'prev_text' => __('«'),
'next_text' => __('»'),
'total' => ceil($total / $numOfItems),
'current' => $page
));
This code is tested and working perfect.

WordPress - How can I target the most recent post and modify it?

I'm working on a shortcode and I'd like to target the most recent "Event". Targeting this most recent "Event" I'd like to do two things.
Being able to apply a specific class to it so I can style it differently (I don't want to use first-child).
Add the_excerpt() to it.
Currently the shortcode pulls the most recent 4 "Events". So the most recent would need what I mentioned above. I'd imagine it would have to do something with "count" but I'm not entirely sure, still trying to learn this stuff.
add_shortcode( 'show_events', 'events_query' );
function events_query() {
$args = array(
'posts_per_page' => 4,
'category_name' => 'events',
);
$events_query = new WP_Query( $args );
if ( $events_query->have_posts() ) :
$html_out = '<article>';
while ( $events_query->have_posts() ) :
$events_query->the_post();
// Do stuff with each post here
$html_out .= '<div class="events-item"><div class="meta-date">' . Date('F j, Y') . '</div><div class="meta-info"><div class="meta-title"><h4>' . get_the_title() . '</h4></div>/div></div>';
endwhile;
$html_out .= '</article>';
else : // No results
$html_out = 'No Events Found.';
endif;
wp_reset_query();
return $html_out;
}
EDIT
Updated using code from an answer:
add_shortcode( 'show_events', 'events_query' );
function events_query() {
$args = array(
'posts_per_page' => 4,
'category_name' => 'events',
);
$events_query = new WP_Query( $args );
if ( $events_query->have_posts() ) :
$html_out = '<article>';
$counter = 0;
$event_class = 'events-item';
while ( $events_query->have_posts() ) :
if ( $counter == 0 ) {
$event_class = 'events-item most-recent';
}
$events_query->the_post();
// Do stuff with each post here
$html_out .= '<div class="' . $event_class . '"><div class="meta-date">' . Date('F j, Y') . '</div><div class="meta-info"><div class="meta-title"><h5>' . get_the_title() . '</h5></div></div>';
if ( $counter == 0 ) {
$html_out .= '<div class="meta-info">' . get_the_excerpt() . '</div>';
}
$html_out .= '</div>';
$counter++;
endwhile;
$html_out .= '</article>';
else : // No results
$html_out = 'No Events Found.';
endif;
wp_reset_query();
return $html_out;
}
Hey Darren it's me again :)
You can use additional var $counter to check if it's the first post
Here is the code
add_shortcode( 'show_events', 'events_query' );
function events_query() {
$args = array(
'posts_per_page' => 4,
'category_name' => 'events',
);
$events_query = new WP_Query( $args );
if ( $events_query->have_posts() ) :
$html_out = '<article>';
$counter = 0;
while ( $events_query->have_posts() ) :
$event_class = 'events-item';
if ( $counter == 0 ) {
$event_class = 'events-item first-event, additiona-classes';
}
$events_query->the_post();
// Do stuff with each post here
$html_out .= '<div class="' . $event_class . '"><div class="meta-date">' . Date('F j, Y') . '</div><div class="meta-info"><div class="meta-title"><h4>' . get_the_title() . '</h4></div>/div>';
if ( $counter == 0 ) {
$html_out .= '<div class="meta-info">' . $post->post_excerpt . '</div>';
}
$html_out .= '</div>';
$counter++;
endwhile;
$html_out .= '</article>';
else : // No results
$html_out = 'No Events Found.';
endif;
wp_reset_query();
return $html_out;
}
Hello Dear please use the if codition
add_shortcode( 'show_events', 'events_query' );
function events_query() {
$args = array(
'posts_per_page' => 4,
'category_name' => 'events',
);
$events_query = new WP_Query( $args );
if ( $events_query->have_posts() ) :
$html_out = '<article>';
$i=1;
while ( $events_query->have_posts() ) :
$events_query->the_post();
if($i==1){
$class = "new_class";
$excerpt = the_excerpt();
}
$html_out .= '<div class="events-item'.$class."><div class="meta-date">' . Date('F j, Y') . '</div><div class="meta-info"><div class="meta-title"><h4>' . get_the_title() . '</h4>
<p>'.$excerpt.'</p></div></div></div>';
$i++;
endwhile;
$html_out .= '</article>';
else : // No results
$html_out = 'No Events Found.';
endif;
wp_reset_query();
return $html_out;
}

ob_start() combined with wp_query

I'm making my first plugin and I have a problem with displaying my shortcode.
It shows in the top all the time but I have read some about ob_start(); and trying to use it but the shortcode just returns nothing.
Im using the code below - it seems to have something to do with my posts. Anyone know why how to solve this problem?
My WP_Query:
$query = new WP_Query( array(
'category__in' => $categories,
'posts_per_page' => $whpost_stored_meta_shortcode['whpost_number_of_posts'][0]
));
The code i use to display it:
ob_start();
echo '<div class="whpost_content">';
while($query->have_posts()) : $query->the_post();
// Get the URL to the attached image.
$attached_image = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'large' );
echo '<div class="whpost_post" style="width: ' . $post_width .'; background-image: url(' . $attached_image[0] . ');">';
the_title();
echo '</div>';
endwhile;
echo '</div>';
return ob_get_clean();
The complete code for the shortcode function:
<?php
function cpt_content_func( $atts ) {
// Get the ID we putted in into [wh-postgrid id="THIS_ID"]
extract( shortcode_atts( array(
'id' => null
), $atts ) );
// Get stored meta data (For the categories - needed to be formatted in
// a certain way)
$categories = get_post_meta( $id, 'whpost_cats', true );
// Get meta data for settings and such.
$whpost_stored_meta_shortcode = get_post_meta( $id );
// Get the correct categories and use the settings we got.
$query = new WP_Query( array(
'category__in' => $categories,
'posts_per_page' => $whpost_stored_meta_shortcode['whpost_number_of_posts'][0]
));
// Set the styles
switch ( $whpost_stored_meta_shortcode['whpost_posts_per_line'][0] ) {
case 1:
$post_width = '100%';
$post_max_height = '';
break;
case 2:
$post_width = '50%';
$post_max_height = '';
break;
case 3:
$post_width = '33.333333%';
$post_max_height = '';
break;
case 4:
$post_width = '25%';
$post_max_height = '';
break;
default:
$post_width = '50%';
}
// Display the front-end
ob_start();
echo '<div class="whpost_content">';
while($query->have_posts()) : $query->the_post();
// Get the URL to the attached image.
$attached_image = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'large' );
echo '<div class="whpost_post" style="width: ' . $post_width .'; background-image: url(' . $attached_image[0] . ');">';
the_title();
echo '</div>';
endwhile;
echo '</div>';
return ob_get_clean();
}
add_shortcode('wh-postgrid','cpt_content_func');
I think problem with your Query, you can add this line to check
if (!$query->have_posts()) {
return 'Empty result';
} else {
return ob_get_clean();
}
The function must return the output.
Change your code like this and it should work:
$output = '<div class="whpost_content">';
while($query->have_posts()) : $query->the_post();
// Get the URL to the attached image.
$postId = get_the_ID();
$attached_image = wp_get_attachment_image_src( get_post_thumbnail_id( $postId ), 'large' );
$output .= '<div class="whpost_post" style="width: ' . $post_width .'; background-image: url(' . $attached_image[0] . ');">';
$output .= get_the_title( $postId );
$output .= '</div>';
endwhile;
$output .= '</div>';
return $output;

WordPress - User Query - Order by Meta Value

I am trying to use a WordPress User Query to create a list of users that is ordered by a custom meta value. Its a simple numeric value, going from 1 to 100, so 1 needs to be displayed first, 100 last, etc.
This is my attempt, which has failed miserably:
<?php
$args = array(
'role' => 'Author',
'meta_key' => 'order-number',
'orderby' => 'order-number',
'order' => 'asc',
);
$wp_user_query = new WP_User_Query($args);
$authors = $wp_user_query->get_results();
if (!empty($authors))
{
echo '<div style="float:left;">';
foreach ($authors as $author)
{
$author_info = get_userdata($author->ID);
echo '<div class="post team-member"><div class="image">';
echo get_avatar( $author_info->ID, 91 );
echo '</div>';
echo '<div class="content"><div class="title">' . $author_info->user_firstname . ' ' . $author_info->user_lastname . '</div>';
echo '' . substr( get_the_author_meta('user_description',$author_info->ID) , 0 , 100 ) . '...';
echo '</div></div>';
}
echo '</div>';
} else {
echo 'No authors found';
}
?>
I think the problem is that wp_user_query does not support custom fields in orderby - so I need a solution that works around this.
Any ideas?
Correct. The orderby parameter can only accept possible values of 'login' (default), 'nicename', 'email', 'url', and 'registered'.
A dirty fix would be something like this:
<?php
global $wpdb; //Ignore this line if you're not within a function
$order = $wpdb->get_results("SELECT DISTINCT user_id FROM $wpdb->usermeta WHERE meta_key='order-number' ORDER BY meta_value ASC", "ARRAY_N");
$authors = array();
foreach($order as $aid)
$authors[] = new WP_User($aid[0]);
if (!empty($authors))
{
echo '<div style="float:left;">';
foreach ($authors as $author)
{
$author_info = get_userdata($author->ID);
echo '<div class="post team-member"><div class="image">';
echo get_avatar( $author_info->ID, 91 );
echo '</div>';
echo '<div class="content"><div class="title">' . $author_info->user_firstname . ' ' . $author_info->user_lastname . '</div>';
echo '' . substr( get_the_author_meta('user_description',$author_info->ID) , 0 , 100 ) . '...';
echo '</div></div>';
}
echo '</div>';
} else {
echo 'No authors found';
}
This is largely untested, so I can't guarantee this will work straight out of the gate, but it should get you started.
Hope this helps!
<?php
// prepare arguments this is your query.
$args = array(
'meta_key' => 'last_name',
'query_id' => 'wps_last_name',
);
// Create the WP_User_Query object
$author_query = new WP_User_Query( $args );
?>
Add following lines to functions.php file
<?php
add_action( 'pre_user_query', 'wps_pre_user_query' );
/*
* Modify the WP_User_Query appropriately
*
* Checks for the proper query to modify and changes the default user_login for $wpdb->usermeta.meta_value
*
* #param WP_User_Query Object $query User Query object before query is executed
*/
function wps_pre_user_query( &$query ) {
global $wpdb;
if ( isset( $query->query_vars['query_id'] ) && 'wps_last_name' == $query->query_vars['query_id'] )
$query->query_orderby = str_replace( 'user_login', "$wpdb->usermeta.meta_value", $query->query_orderby );
}
?>

Resources