Display only author posts in author page in Wordpress - wordpress

What should I do to display only a certain author posts, in my author.php page? I don't mean an specific author (using author ID), but the default page for authors. With the current code, its displaying posts from all the other authors. Should I use a specific query?
<?php $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$custom_args = array(
'post_type' => 'post',
// 'order' => 'DESC',
'posts_per_page' => 10,
'paged' => $paged
);
// custom query
$recent_posts = new WP_Query($custom_args);
// check that we have results
if($recent_posts->have_posts()) : ?>
<ul class="article_list">
<?php
// start loop
while ($recent_posts->have_posts() ) : $recent_posts->the_post(); ?>
<li class="regular">
<a href="<?php echo get_permalink(); ?>">
<div class="text">
<p class="category"><?php foreach((get_the_category()) as $category) { echo $category->cat_name . ' '; } ?></p>
<p class="autor"><?php the_author(); ?></p>
<h3 class="article_title"><?php echo mb_strimwidth(get_the_title(), 0, 80, '...'); ?></h3>
<p class="date"><?php echo get_the_date( 'Y-m-d' ); ?></p>
</div>
<div class="mask">
<?php
$url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
echo '<div class="art_img" style="background: url('. $url.')"></div>';
?>
</div>
</a>
</li>
<?php endwhile; ?>
</ul>
<?php endif;
echo '<div class="pagination">';
if(function_exists('wp_pagenavi')) { wp_pagenavi(array( 'query' => $recent_posts )); }
echo '</div>';
wp_reset_postdata();
?>

You can use get_posts function of wordpress.
Here is reference:
https://codex.wordpress.org/Template_Tags/get_posts
Pass the required authors Id in the argument list.

Hello just add global $current_user; above $paged variable on top of your provided snippet and add 'author' => $current_user->ID, in your array of $custom_args so it will display posts of specific user in author page

Add author__in into your query
Try this
<?php $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$custom_args = array(
'post_type' => 'post',
'author__in'=> array(2,4,6), //Authors's id's you like to include
// 'order' => 'DESC',
'posts_per_page' => 10,
'paged' => $paged
);

Related

Wordpress Pagination not working - Page url changes but content doesn't change

Homepage displays all the post and I want pagination for this. After clicking on the pagination link page number changes in url but same content is shown on all pages, also the pagination link which number shows only 1 selected. Like if i selcted pagination link 3, url shown page/3, but page number is 1 and content doesnt even change.
<?php
/**
* Genesis Sample.
*
* This file adds the landing page template to the Genesis Sample Theme.
*
* Template Name: Home
*
* #package Genesis Sample
* #author StudioPress
* #license GPL-2.0-or-later
* #link https://www.studiopress.com/
*/
get_header();
$paged = (isset($_GET['paged'])) ? intval($_GET['paged']) : 1;
$paged = (get_query_var('paged')) ? get_query_var('paged') : $paged;
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 9,
'paged' => $paged,
);
$wp_query = new WP_Query($args);
?>
<div class="body-container">
<div class="blogs-banner">
<?php
$image = get_field('image');
if (!empty($image)) : ?>
<img src="<?php echo esc_url($image['url']); ?>" alt="<?php echo esc_attr($image['alt']); ?>" class="img-main" />
<?php endif; ?>
</div>
<div class="about-container">
<div class="blog-list">
<div class="row-same-height" id="postContainer" data-type="post" data-count="<?php echo $wp_query->found_posts; ?>">
<?php
if ( $wp_query->have_posts() ) :
while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
<a href="" class="link-post">
<div class="col-same-height">
<div class="content">
<div class="TopArticleCard-Container">
<div class="img-container">
<?php
$thumbnail = get_post_meta(get_the_id(), 'large_img', true);
if ( $thumbnail ) { ?>
<img src="<?php echo $thumbnail; ?>" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" loading="lazy"/>
<?php } else if ( has_post_thumbnail() ) {
the_post_thumbnail('large', ['title' => get_the_title()], ['alt' => get_the_title()]); ?>
<?php
} else { ?>
<img src="<?php echo get_site_url(); ?>/wp-content/uploads/2020/09/default.jpg" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" loading="lazy"/>
<?php } ?>
</div>
<div class="article-description">
<div class="article-tag">
<?php $cat = get_the_category(); echo $cat[0]->cat_name; ?>
</div>
<div class="content">
<div class="article-title">
<?php the_title(); ?>
</div>
</div>
</div>
</div>
</div>
</div>
</a>
<?php endwhile;
// post_pagination($query);
endif;
wp_reset_postdata();
?>
</div>
<nav class="pagination-nav">
<ul class="pagination">
<?php
if ( $wp_query->have_posts() ) :
$currentPage = (get_query_var('paged')) ? get_query_var('paged') : 1;
$pagination_args = array(
'base' => str_replace(999999999, '%#%', esc_url(get_pagenum_link(999999999))),
'format' => '/page/%#%',
'current' => $currentPage,
'total' => $wp_query->max_num_pages,
'prev_text' => 'Prev',
'next_text' => 'Next',
);
echo paginate_links(array_merge($pagination_args, array('query' => $wp_query)));
endif;
?>
</ul>
</nav>
</div>
</div>
</div>
</div>
<?php get_footer();
It seems that the issue you are facing is related to the query for fetching the posts and the pagination links. Here are a few things you can try to fix this issue:
Check if the paged parameter is set correctly in the URL: In the code you provided, you are using $_GET['paged'] to get the current page number, but in the pagination links, you are using get_query_var('paged'). Make sure that the URL of the pagination links contains the paged parameter, for example, http://example.com/page/3/?paged=3.
Make sure to use the correct query object for pagination links: In the code you provided, you are using $wp_query to generate the pagination links. However, this is not the correct query object to use if you are using a custom WP_Query. Instead, you should use the query object for the custom WP_Query, which is $query in this case.
Here's the updated code with these changes:
$paged = (isset($_GET['paged'])) ? intval($_GET['paged']) : 1;
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 9,
'paged' => $paged,
);
$query = new WP_Query($args);
if ($query->have_posts()) :
while ($query->have_posts()) : $query->the_post();
// Display the post content
endwhile;
endif;
// Generate pagination links
if ($query->max_num_pages > 1) :
$current_page = max(1, get_query_var('paged'));
echo '<div class="pagination">';
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => '/page/%#%',
'current' => $current_page,
'total' => $query->max_num_pages,
));
echo '</div>';
endif;
wp_reset_postdata();

post per page is not working with sticky post query in wordpress

this is my post loop that i am using
<?php
$sticky = get_option( 'sticky_posts' );
rsort( $sticky );
$args = array(
'post_type' => 'post',
'post__in' => $sticky,
'posts_per_page' => 1
);
$sticky_query = new WP_Query( $args );
while ( $sticky_query->have_posts() ) : $sticky_query->the_post();
?>
<article class="cust-arc-post">
<img src="<?php the_post_thumbnail_url(); ?>" alt="">
<div class="arc-post-header">
<h3><?php the_title(); ?></h3>
<a class="cat" href="javascript:;">Category Title</a>
</div>
<p><?php echo wp_trim_words( get_the_content(), 20, '...' ); ?></p>
</article>
<?php endwhile;
wp_reset_postdata();
?>
i tried using offset but no luck,
i think something is wrong with my loop
if anyone can help me with this
thanks in advance
All the information you need can be found in the theme handbook
As stated in the codex, this displays just the first sticky post, if none return the last post published:
$args = array(
'posts_per_page' => 1,
'post__in' => get_option( 'sticky_posts' ),
'ignore_sticky_posts' => 1
);
$query = new WP_Query( $args );
Your problem likely lies in the rsort(); function, because it's reversing the array from highest to lowest.
Try the below code.
<?php
$sticky = get_option( 'sticky_posts' );
rsort( $sticky );
$posts_per_page = 12;
$sticky_count = count($sticky);
if ($sticky_count < $posts_per_page) {
$posts_per_page = $posts_per_page - $sticky_count;
} else {
$posts_per_page = 1;
}
$args = array(
'post_type' => 'post',
'post__in' => $sticky,
'posts_per_page' => $posts_per_page
);
$sticky_query = new WP_Query( $args );
while ( $sticky_query->have_posts() ) : $sticky_query->the_post(); ?>
<article class="cust-arc-post">
<img src="<?php the_post_thumbnail_url(); ?>" alt="">
<div class="arc-post-header">
<h3><?php the_title(); ?></h3>
<a class="cat" href="javascript:;">Category Title</a>
</div>
<p><?php echo wp_trim_words( get_the_content(), 20, '...' ); ?></p>
</article>
<?php endwhile; wp_reset_postdata(); ?>

Wordpress get_posts by category

I have the following bit of code:
$args = array(
'posts_per_page' => -1,
'category' => 7,
'orderby' => 'name',
'order' => 'ASC',
'post_type' => 'product'
);
$posts = get_posts($args);var_dump($posts);
This should return one post I know that is in the category, but it isn't. If I leave out the 'category'-argument, I get all the products, so I know this should normally work. If I change the category to 1 and take out my custom post type (product), I get my default posts.
I can't see what's wrong with this. Can anyone spot what the problem is?
If you'd like you could accomplish the same thing but with WP_Query. This gets the post type "Product" with "posts_per_page" for the amount of posts, and "product_cat" for the product category. Hope this helps!
EDIT: If you'd like to do it your way maybe instead of "category" try "product_cat"
<ul class="products">
<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => 1, 'product_cat' => 'shoes', 'orderby' => 'rand' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<h2>Shoes</h2>
<li class="product">
<a href="<?php echo get_permalink( $loop->post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">
<?php woocommerce_show_product_sale_flash( $post, $product ); ?>
<?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="300px" height="300px" />'; ?>
<h3><?php the_title(); ?></h3>
<span class="price"><?php echo $product->get_price_html(); ?></span>
</a>
<?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?>
</li>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</ul><!--/.products-->

wordpress 404 after click on pagination links

i have a problem with the pagination in wordpress. I'm having a category-template wich involkes a query for a custom posts. The problem is that when i add a pagination and you go to the next page the pagination add $_GET['paged'] but the template reject it and trow 404 page. I also discovered that if $_GET['paged']=1 everything works fine but if it is 2 or more throw 404 page. The thing is that i try in the index loop and it also does not work. I'm using html5blank theme.
Here is the code to the query:
<section id="inner-pad">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
//'posts_per_page' => 6,
'post_type' => 'menu-pub',
'cat' =>3,
'paged' => $paged,
'order'=>'DESC'
);
$wp_query = new WP_Query( $args );
$i=1;
while ( $wp_query->have_posts() )
{
$wp_query->the_post();
?>
<article class="article-first-vision <?= ($i==2)?'mar':'' ?>" >
<?php if ( has_post_thumbnail()) { ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php the_post_thumbnail('menu-pub'); ?>
</a>
<?php
}; ?>
<h2>
<?php the_title(); ?>
</h2>
<p>
<?php the_content(); ?>
</p>
</article>
<?php
if($i==3){$i=0;}enter code here
$i++;
}
?>
</section>
<?php
$big = 999999999;
echo paginate_links(array(
'base' => str_replace($big, '%#%', get_pagenum_link($big)),
'format' => '?paged=%#%',
'current' => max(1, get_query_var('paged')),
'total' => $wp_query->max_num_pages
));
?>
Here is a new dialog where i put a theme for this:
http://wordpress.org/support/topic/custom-post-and-category-template-pagination-problem?replies=2#post-4560385
try to add global $wp_query before paginate_links()
Hi here is the answer to my question:
add_action( 'parse_query','changept' );
function changept() {
if( is_category() && !is_admin() )
set_query_var( 'post_type', array( 'menu-pub', 'new-pub', 'read-pub', 'awards-pub', 'live-pub','post' ));
return;
}

Adding pagination to custom post loop in page

I have created a custom page template (testimonials-page.php) and in that template I am
loading custom post type 'testimonials' using the following loop:
<?php query_posts(array(
'posts_per_page' => 5,
'post_type' => 'testimonials',
'orderby' => 'post_date',
'paged' => $paged
)
); ?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" class="quote">
<?php echo get_the_post_thumbnail($id, array($image_width,$image_height)); ?>
<?php the_content(); ?>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
How do I add pagination to that? I installed the WP Paging plugin, and while that plugin works
great when I call the pagination into category.php using:
<p><?php wp_paging(); ?></p>
Inserting the same thing into testimonial-page.php results in broken formatting and links that
404 on me.
Firstly, never EVER use query_posts unless your intention is to modify the default Wordpress Loop.
Instead, switch to WP Query.
Here's something I wrote for a theme I did for a client using all built-in Wordpress functions. It's been working pretty well for me so far, so I'll integrate it into your code as best as I can:
global $paged;
$curpage = $paged ? $paged : 1;
$args = array(
'post_type' => 'testimonials',
'orderby' => 'post_date',
'posts_per_page' => 5,
'paged' => $paged
);
$query = new WP_Query($args);
if($query->have_posts()) : while ($query->have_posts()) : $query->the_post();
?>
<div id="post-<?php the_ID(); ?>" class="quote">
<?php
echo get_the_post_thumbnail($post->ID, array($image_width,$image_height));
the_content();
?>
</div>
<?php
endwhile;
echo '
<div id="wp_pagination">
<a class="first page button" href="'.get_pagenum_link(1).'">«</a>
<a class="previous page button" href="'.get_pagenum_link(($curpage-1 > 0 ? $curpage-1 : 1)).'">‹</a>';
for($i=1;$i<=$query->max_num_pages;$i++)
echo '<a class="'.($i == $curpage ? 'active ' : '').'page button" href="'.get_pagenum_link($i).'">'.$i.'</a>';
echo '
<a class="next page button" href="'.get_pagenum_link(($curpage+1 <= $query->max_num_pages ? $curpage+1 : $query->max_num_pages)).'">›</a>
<a class="last page button" href="'.get_pagenum_link($query->max_num_pages).'">»</a>
</div>
';
wp_reset_postdata();
endif;
?>
Jan 2018 Edit:
Also consider using paginate_links, since it's also built into Wordpress, and has more robust options and capabilities.
Try this code for custom loop with pagination:
<?php
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) { // 'page' is used instead of 'paged' on Static Front Page
$paged = get_query_var('page');
} else {
$paged = 1;
}
$custom_query_args = array(
'post_type' => 'post',
'posts_per_page' => get_option('posts_per_page'),
'paged' => $paged,
'post_status' => 'publish',
'ignore_sticky_posts' => true,
//'category_name' => 'custom-cat',
'order' => 'DESC', // 'ASC'
'orderby' => 'date' // modified | title | name | ID | rand
);
$custom_query = new WP_Query( $custom_query_args );
if ( $custom_query->have_posts() ) :
while( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
<article <?php post_class(); ?>>
<h3><?php the_title(); ?></h3>
<small><?php the_time('F jS, Y') ?> by <?php the_author_posts_link() ?></small>
<div><?php the_excerpt(); ?></div>
</article>
<?php
endwhile;
?>
<?php if ($custom_query->max_num_pages > 1) : // custom pagination ?>
<?php
$orig_query = $wp_query; // fix for pagination to work
$wp_query = $custom_query;
?>
<nav class="prev-next-posts">
<div class="prev-posts-link">
<?php echo get_next_posts_link( 'Older Entries', $custom_query->max_num_pages ); ?>
</div>
<div class="next-posts-link">
<?php echo get_previous_posts_link( 'Newer Entries' ); ?>
</div>
</nav>
<?php
$wp_query = $orig_query; // fix for pagination to work
?>
<?php endif; ?>
<?php
wp_reset_postdata(); // reset the query
else:
echo '<p>'.__('Sorry, no posts matched your criteria.').'</p>';
endif;
?>
Source:
WordPress custom loop with pagination

Resources