• Resolved mmkh

    (@mmkh)


    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 below

    category.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 WHERE
    option_name LIKE ('_transient%_wpse231557_%')" );
    $wpdb->query( "DELETE FROM $wpdb->options WHERE option_name LIKE ('_transient_timeout%_wpse231557_%')" );
    });

    page category.php

    section img

    The page I need help with: [log in to see the link]

Viewing 6 replies - 1 through 6 (of 6 total)
  • The problem arises from the way you store the data in the transient. Only the 1st WP_User object ends up there, not the array.

    I would recommend changing the transient. When saving, just take the IDs with you:

    // Get a unique array of ids
    $post_author_ids = array_unique($post_author_ids);

    set_transient( $transient_name, $post_author_ids, 72 * HOUR_IN_SECONDS );

    Only create the user object when reading from the ID:

    $users = get_transient( $transient_name )
    foreach ( $users as $user_id ) {
    $user = get_user_by( 'ID', $user_id );
    // …

    Tip: to debug your code, work with var_dump();exit; to find out what value a variable has at a certain point. This way you can quickly find things like this.

    Thread Starter mmkh

    (@mmkh)

    Hi threadi, I hope you are doing well. Unfortunately, I tried to figure out what to do but I didn’t really get it. I realize this is taking up a lot of your time, but I was wondering if you could be kind enough to implement (place) what you mean in my code itself? That way I might understand what you mean and figure out where my problem was.

    Here is the code I used to test this:

    // Check if transient is set
    if ( false === ( $users = 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 );

    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 );

    // Get a unique array of ids
    $post_author_ids = array_unique($post_author_ids);

    set_transient( $transient_name, $post_author_ids, 72 * HOUR_IN_SECONDS );
    }

    }

    if ( false !== $users ) {
    foreach ( $users as $user_id ) {
    $user = get_user_by( 'ID', $user_id );
    // ..
    }
    }

    The rest of the code is then unchanged.

    Thread Starter mmkh

    (@mmkh)

    Hello, thank you for your answer, but it still didn’t work, but I solved it, of course, with my own method, hoping that this answer will help someone in the future. I will write the solution below. I will write two solutions. One directly retrieves information from the database. This is faster than the second method, but its security and compatibility with WordPress will be less in the future. So if you are looking for a more attractive solution, use the first code, and if you are looking for a more reliable solution, use the second code.

    Thread Starter mmkh

    (@mmkh)

    code 1

    <?php
    if ( is_category() || is_tag() ) { // بررسی اینکه آیا در صفحه آرشیو دسته‌بندی یا برچسب هستیم
    $current_term = get_queried_object(); // گرفتن شیء دسته‌بندی یا برچسب فعلی

    global $wpdb;

    // کوئری برای دریافت نویسندگان و تعداد پست‌های هر نویسنده در دسته‌بندی یا برچسب جاری
    $sql = "
    SELECT p.post_author, COUNT(p.ID) AS post_count
    FROM $wpdb->posts p
    INNER JOIN $wpdb->term_relationships tr ON p.ID = tr.object_id
    INNER JOIN $wpdb->term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
    WHERE tt.taxonomy = %s
    AND tt.term_id = %d
    AND p.post_status = 'publish'
    GROUP BY p.post_author
    ORDER BY post_count DESC
    ";

    // اجرای کوئری برای دریافت نویسندگان و تعداد پست‌ها
    $results = $wpdb->get_results( $wpdb->prepare( $sql, $current_term->taxonomy, $current_term->term_id ) );

    // بررسی اینکه آیا نتایجی داریم
    if ($results) {
    foreach ($results as $row) {
    $author_id = $row->post_author;
    $post_count = $row->post_count;
    $user = get_user_by('ID', $author_id);

    ?>
    <div>
    <div>
    <a href="<?php echo get_author_posts_url($author_id); ?>">
    <div>
    <!-- نمایش تصویر نویسنده -->
    <div class="author-avatar">
    <img src="<?php echo get_avatar_url($author_id); ?>" alt="<?php echo esc_attr($user->display_name); ?>" />
    </div>
    <img src="<?php echo get_avatar_url($author_id); ?>" alt="<?php echo esc_attr($user->display_name); ?>" />
    </div>
    </a>
    <div>
    <a href="<?php echo get_author_posts_url($author_id); ?>">
    <h3><?php echo esc_html($user->display_name); ?></h3>
    </a>
    <p><?php echo esc_html($user->first_name); ?> تا کنون <span> <?php echo esc_html($post_count); ?> </span> محتوا نوشته!</p>
    </div>
    </div>
    <div>
    <a href="<?php echo get_author_posts_url($author_id); ?>">
    <button>
    سایر محتوا های <?php echo esc_html($user->first_name); ?> را بخوانید
    <!-- 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
    }
    } else {
    echo 'هیچ نویسنده‌ای برای این دسته‌بندی یا برچسب وجود ندارد.';
    }
    }
    ?>
    Thread Starter mmkh

    (@mmkh)

    code 2

    <?php
    // if ( is_category() || is_tag() ) {
    // $current_term = get_queried_object();
    //
    // // آرگومان‌های WP_Query برای دریافت پست‌های مرتبط با ترم جاری
    // $args = [
    // 'post_type' => 'post',
    // 'post_status' => 'publish',
    // 'tax_query' => [
    // [
    // 'taxonomy' => $current_term->taxonomy,
    // 'terms' => $current_term->term_id,
    // 'field' => 'term_id',
    // ],
    // ],
    // 'posts_per_page' => -1,
    // 'cache_results' => false,
    // 'update_post_meta_cache' => false,
    // 'update_post_term_cache' => false,
    // 'suppress_filters' => false,
    // ];
    // //
    // // اجرای کوئری با استفاده از WP_Query
    // $query = new WP_Query( $args );
    //
    // // شمارش پست‌ها بر اساس نویسنده
    // $authors = [];
    // if ( $query->have_posts() ) {
    // while ( $query->have_posts() ) {
    // $query->the_post();
    //
    // $author_id = get_the_author_meta( 'ID' );
    // if ( isset( $authors[ $author_id ] ) ) {
    // $authors[ $author_id ]++; // افزایش تعداد پست‌ها
    // } else {
    // $authors[ $author_id ] = 1; // شروع شمارش
    // }
    // }
    // wp_reset_postdata(); // بازنشانی وضعیت پس از کوئری
    // }
    //
    // // مرتب‌سازی نویسندگان بر اساس تعداد پست‌ها
    // arsort( $authors );
    //
    // // نمایش اطلاعات نویسندگان
    // if ( ! empty( $authors ) ) {
    // foreach ( $authors as $author_id => $post_count ) {
    // $user = get_userdata( $author_id );
    //
    // if ( $user ) { // بررسی معتبر بودن نویسنده
    // ?>
    <!-- <div class="author-box">-->
    <!-- <a href="--><?php //echo esc_url( get_author_posts_url( $author_id ) ); ?><!--">-->
    <!-- <div class="author-info">-->
    <!-- <div class="author-avatar">-->
    <!-- <img src="--><?php //echo esc_url( get_avatar_url( $author_id ) ); ?><!--" alt="--><?php //echo esc_attr( $user->display_name ); ?><!--" />-->
    <!-- </div>-->
    <!-- <div class="author-name">-->
    <!-- <h3>--><?php //echo esc_html( $user->display_name ); ?><!--</h3>-->
    <!-- </div>-->
    <!-- </div>-->
    <!-- </a>-->
    <!-- <p>--><?php //echo esc_html( $user->first_name ); ?><!-- تا کنون <span>--><?php //echo esc_html( $post_count ); ?><!--</span> پست نوشته است.</p>-->
    <!-- <a href="--><?php //echo esc_url( get_author_posts_url( $author_id ) ); ?><!--">-->
    <!-- <button>مشاهده پست‌های --><?php //echo esc_html( $user->display_name ); ?><!--</button>-->
    <!-- </a>-->
    <!-- </div>-->
    <!-- --><?php
    // }
    // }
    // } else {
    // echo '<p>هیچ نویسنده‌ای برای این دسته‌بندی یا برچسب وجود ندارد.</p>';
    // }
    // }
    // ?>

    Neither of these two solutions requires Kurdish in the function file (unlike the first solution).

    😊

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘The function I wrote is not working properly.’ is closed to new replies.