• Resolved jspenney

    (@jspenney)


    There is a difference in order between the WPPP admin page and the pages that are pulled by my code. Can someone give me any ideas for the difference? Code follows.

    The WPS Agenda Needs Strong Wills to Counter Rough Headwinds
    210 views
    Russia Praises Trump but Heaps Scorn on Its EU Neighbors
    209 views
    [UN: Oct. 3] Hamas to Release Hostages, Older People’s Rights & More
    29 views
    Madam SG: Can the UN Finally Smash Its Glass Ceiling?
    20 views
    The Case of Ruben Vardanyan, a Travesty of Justice
    12 views

    Rank 1: The UN’s Gender Parity Goals: The Backlash Begins (ID: 27161, Type: post)
    Rank 2: Could the Taliban Join the UN? They’ll Need to End Their ‘Gender Apartheid’ System (ID: 75046, Type: post)
    Rank 3: The WPS Agenda Needs Strong Wills to Counter Rough Headwinds (ID: 93650, Type: post)
    Rank 4: Russia Praises Trump but Heaps Scorn on Its EU Neighbors (ID: 93688, Type: post)
    Rank 5: No Nobel Peace Prize for President Trump (ID: 86972, Type: post)

    /**
    * Retrieves the top 5 popular post IDs (post and article) from the last 7 days,
    * excludes specific IDs, and caches the result for 12 hours.
    *
    * @return array Array of popular post IDs, or an empty array on failure.
    */
    function cwpai_get_popular_posts_ids_weekly() {

    // Define the transient key and cache time (12 hours)
    $transient_key = ‘cwpai_popular_posts_ids_weekly’;
    $cache_time = 12 * HOUR_IN_SECONDS;

    // Attempt to get cached data
    $post_ids = get_transient( $transient_key );

    // If no cache, generate the data
    if ( false === $post_ids ) {

    $post_ids = array();

    // 1. Verify WPP Query Class exists
    if ( class_exists( ‘\WordPressPopularPosts\Query’ ) ) {

    $exclude_ids = ‘16817,6620,77812,22232,39656’;

    $query_args = array(
    ‘range’ => ‘last7days’,
    ‘limit’ => 5,
    ‘post_type’ => ‘post,article’,
    // FIX: The parameter for excluded IDs is ‘exclude’, not ‘pid’
    ‘exclude’ => $exclude_ids,
    ‘order_by’ => ‘views’,
    );

    // Use the WPP Query class
    $query = new \WordPressPopularPosts\Query( $query_args );
    $popular_posts = $query->get_posts();

    if ( is_array( $popular_posts ) && ! empty( $popular_posts ) ) {
    foreach ( $popular_posts as $popular_post ) {
    $post_ids[] = (int) $popular_post->id;
    }
    }
    } else {
    error_log( ‘WordPress Popular Posts Query class not found.’ );
    return array();
    }

    // Cache the result (even if empty)
    set_transient( $transient_key, $post_ids, $cache_time );
    }

    // Ensure the array only contains the top 5, even if the query returns more/less unexpectedly
    return array_slice( $post_ids, 0, 5 );
    }

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter jspenney

    (@jspenney)

    The first list is WPPP admin page listing and the second i the results of my query

    Thread Starter jspenney

    (@jspenney)

    I figured out the problem

    freshness is the answer.

    Plugin Author Hector Cabrera

    (@hcabrera)

    That, and also:

    • Your cwpai_get_popular_posts_ids_weekly() function (that’s such a long name haha) is caching data for 12 hours whereas the Stats screen is showing live data so that alone will cause a mismatch; and
    • Your cwpai_get_popular_posts_ids_weekly() function is also excluding the following IDs from the list: 16817, 6620, 77812, 22232, 39656. This too will make a difference.

    Hope that helps. Please feel free to let me know if you have any other questions.

Viewing 3 replies - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.