• tandow

    (@tandow)


    Hello,

    I am using the post views counter plugin together with WooCommerce and I am trying to achieve the following:

    Get

    1. the IDs (only) of the most viewed products
    2. of a specific product type and
    3. of a specific time period (for example last 2 weeks).

    By using the tax_query argument together with the pvc_get_most_viewed_posts function of this plugin, I can limit the result of this function to a specific product type.

    Is it possible to pass any query arguments to additionally specify a time period for these view counts? Moreover, is it possible to only output the post IDs instead of post objects?

    Thank you in advance for any responses.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter tandow

    (@tandow)

    I looked through the source code of the plugin and it seems like a time period for the pvc_get_most_viewed_posts function can be specified by using the following query argument:

    'views_query' => array(
      'year'   => '',
      'month'  => '',
      'week'   => '',
      'day'    => '',
      'after'  => '', // can be string or array
      'before' => '', // can be string or array
    }

    By using after and before, it is possible to define a custom time period. The strtotime() function is applied to them when value is a string, i.e. if I want the views from the past 2 weeks:

    'views_query' => array(
      'after'  => '2 weeks ago'
    )

    See includes/class-query.php file for more details.

    Thread Starter tandow

    (@tandow)

    It seems like outputting only post/product IDs is not possible since PVC expects all wp_posts table columns to be selected in the SQL query.

    Moreover, it seems like WooCommerce’s wc_get_products function can be used together with PVC’s query variables like this:

    $output = wc_get_products(
      array(
        'type'   => array('simple'),
        'status' => 'publish',
        'return' => 'objects',
    
        // Required by PVC.
        'fields'           => 'all',
        'suppress_filters' => false,
        'orderby'          => 'post_views',
        'views_query'      => array(
          'after' => '2 weeks ago'
        )
      )
    );

    Setting 'return' => 'objects' outputs an array with product objects.

    Setting 'return' => 'ids' doesn’t output IDs but an array with post objects.

    Please feel free to comment if I am doing something wrong here.

    It would be really helpful to get a report like this!

    Plugin Author dFactory

    (@dfactory)

    Hi,

    We’re going to implement more detailed reporting and CSV/XML export features in the upcoming Pro version of Post Views Counter. This would include reports for a specific post type (like products), but maybe we’ll take into account a possibility of improved reporting for WooCommerce too.

    thanks here it worked with shortcode from the manual.

    Hi, is this code correct to retrieve current day popular posts?

    $today = getdate();
    $args = [
    ‘post_type’ => ‘post’, // Assuming you want blog posts. Change if needed.
    ‘posts_per_page’ => 3, // Adjust to the number of posts you want.
    ‘order’ => ‘DESC’, // Order from highest to lowest views.
    ‘post__not_in’ => [ $post->ID ],
    ‘date_query’ => [
    [
    ‘year’ => $today[‘year’],
    ‘month’ => $today[‘mon’],
    ‘day’ => $today[‘mday’],
    ],
    ],
    ‘orderby’ => ‘post_views’,
    ‘views_query’ => [
    ‘year’ => $today[‘year’],
    ‘month’ => $today[‘mon’],
    ‘day’ => $today[‘mday’],
    ],
    ];

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘How to get most viewed posts of specific time period’ is closed to new replies.