The function I wrote is not working properly.
-
Hello, first of all, thank you for guiding me. I was writing a function that worked as follows:
This function should display all the authors of a category or tag who contributed to its posts
The thing is, I only worked on the category section and haven’t touched on the tag yet!
But what I have prepared does not work properly and the problem is that if even a category has multiple authors my function returns only one author
I realized that if I paginated my posts, for example if I put 10 posts on each page and if I select the authors in such a way that in the first 10 posts the first page is the author of author 1 and in the second page there are 10 posts for author 2, in the initial pagination it introduces author 1 as the author of this category and in the second pagination it announces author 2 as the author, while I want all the authors to be displayed and I could not solve this problem and strangely if a page has two authors it shows two author boxes but with information of only one author! In general it does not follow any particular rule 😂
I will leave you the code used in the function.php , category.php files along with the images of the category.php page belowcategory.php code
<?php
if ( is_category() ) {
$current_category = get_term( $GLOBALS['wp_the_query']->get_queried_object() );
$transient_name = 'wpse231557_' . md5( json_encode( $current_category ) );
// Check if transient is set
if ( false === ( $user_query = get_transient( $transient_name ) ) ) {
$args = [
'wpse_post_author' => true, // To trigger our filter
'posts_per_page' => '10',
'orderby' => 'author',
'order' => 'desc',
'suppress_filters' => false, // Allow filters to alter query
'cache_results' => false, // Do not cache posts
'update_post_meta_cache' => false, // Do not cache custom field data
'update_post_term_cache' => false, // Do not cache post terms
'tax_query' => [
[
'taxonomy' => $current_category->taxonomy,
'terms' => $current_category->term_id,
'include_children' => true
]
]
];
$posts_array = get_posts( $args );
$user_query = false;
if ( $posts_array ) {
// Get all the post authors from the posts
$post_author_ids = wp_list_pluck( $posts_array, 'post_author' );
// Get a unique array of ids
$post_author_ids = array_unique( $post_author_ids );
$user_args = [
'include' => $post_author_ids
];
$user_query = new \WP_User_Query( $user_args );
}
// Set the transient for 3 days, adjust as needed
set_transient( $transient_name, $user_query, 72 * HOUR_IN_SECONDS );
}
if ( false !== $user_query
&& $user_query->results
) {
foreach ( $user_query->results as $user ) {
?>
<div>
<!-- aurthor photo and name -->
<div>
<a href=" <?php
global $post;
$author_id=$post->post_author;
echo get_author_posts_url($author_id);
?> ">
<div>
<div>
<img src="<?php echo get_avatar_url(get_the_author_meta('ID')); ?>">
</div>
<img src="<?php echo get_avatar_url(get_the_author_meta('ID')); ?>">
</div>
</a>
<div>
<a href=" <?php
global $post;
$author_id=$post->post_author;
echo get_author_posts_url($author_id);
?> ">
<h3> <?php echo get_the_author_meta('display_name'); ?> </h3>
</a>
<!-- If you want to include custom post types in the future, you must define an array and add the names of those new post types. -->
<p><?php echo get_the_author_meta('first_name'); ?> تا کنون <span> <?php echo count_user_posts( $author_id, 'post', 'false') ?> </span> محتوا نوشته!</p>
</div>
</div>
<!-- author page button -->
<div>
<a href=" <?php
global $post;
$author_id=$post->post_author;
echo get_author_posts_url($author_id);
?> ">
<button>
سایر محتوا های خسرو را بخوانید
<!-- curve left and right -->
<div><div><span></span><span></span></div></div>
<div><div><span></span><span></span></div></div>
</button>
</a>
</div>
</div>
<?php
}
}
}
?>function.php
add_filter( 'posts_fields', function ( $fields, \WP_Query $q ) use ( &$wpdb )
{
remove_filter( current_filter(), __FUNCTION__ );
// Only target a query where the new wpse_post_author parameter is set to true
if ( true === $q->get( 'wpse_post_author' ) ) {
// Only get the post_author column
$fields = "
$wpdb->posts.post_author
";
}
return $fields;
}, 10, 2);
add_action( 'transition_post_status', function () use ( &$wpdb )
{
$wpdb->query( "DELETE FROM $wpdb->options WHEREoption_name
LIKE ('_transient%_wpse231557_%')" );
$wpdb->query( "DELETE FROM $wpdb->options WHEREoption_name
LIKE ('_transient_timeout%_wpse231557_%')" );
});page category.php
section img
The page I need help with: [log in to see the link]
- The topic ‘The function I wrote is not working properly.’ is closed to new replies.