I have this code:
// Posts or Portfolio Widget
add_action( 'elementor/query/my_custom_filter', function( $query ) {
// Modify the posts query here
} );
source: https://developers.elementor.com/custom-query-filter/
and need to call my_custom_filter query to sort by this:
get_option( 'stats_cache' );
which would array something like:
array(
['7375996b7a989f95a6ed03ca7c899b1f'] => array(
[1353440532] => array(
[0] => array(
['post_id'] => 0
['post_title'] => 'Home page'
['post_permalink'] => 'http://www.example.com/'
['views'] => 1132
)
[1] => array(
['post_id'] => 4784
['post_title'] => 'Hello World!'
['post_permalink'] =>
['views'] => 493
)
/* till item [9] */
or this (I am hosted on wp and run jetpack --- this is preferred actually):
if( function_exists( 'stats_get_csv' ) ) {
$top_posts = stats_get_csv( 'postviews', 'period=month&limit=30' );
}
which would return this:
array(
[0] => array(
['post_id'] => 0
['post_title'] => 'Home page'
['post_permalink'] => 'http://www.example.com/'
['views'] => 6806
)
[1] => array(
['post_id'] => 8005
['post_title'] => 'Hello World!'
['post_permalink'] =>
['views'] => 1845
)
/* till item [29] */
source: How to query the number of view counts for a post in Wordpress JetPack?
I have created the custom query ID -
( 'elementor/query/my_custom_filter', function( $query )
but I am not sure how to use these 2 functions:
get_option( 'stats_cache' );
- if( function_exists( 'stats_get_csv' ) ) {
$top_posts = stats_get_csv( 'postviews', 'period=month&limit=30' );
}
or where/how to insert them here:
// Posts or Portfolio Widget
add_action( 'elementor/query/my_custom_filter', function( $query ) {
// Modify the posts query here
} );
for the query to work.
I have tried to paste the jetpack function inside but that didn't work and I have tried to 'orderby', $top_posts
or call $top_posts = $query
but no success and a bunch of other stuff but nothing worked, for sure not doing something right here.
My Answer:
To use the `get_option('stats_cache')` or `stats_get_csv()` functions within the `elementor/query/my_custom_filter` action, you need to retrieve the data from these functions and then modify the query based on that data.
Here's an example of how you can achieve this:
php
add_action( 'elementor/query/my_custom_filter', function( $query ) {
if( function_exists( 'stats_get_csv' ) ) {
$top_posts = stats_get_csv( 'postviews', 'period=month&limit=30' );
// Check if the top_posts array is not empty
if ( ! empty( $top_posts ) ) {
$post_ids = array_column( $top_posts, 'post_id' );
// Modify the query to order by the post IDs in the top_posts array
$query->set( 'post__in', $post_ids );
$query->set( 'orderby', 'post__in' );
}
}
} );
In this code snippet:
1. We first check if the `stats_get_csv()` function exists.
2. We retrieve the top posts data using `stats_get_csv()`.
3. We extract the post IDs from the top posts data array using `array_column()`.
4. We modify the query to only include posts with IDs from the top posts array and order them by the post IDs.
Make sure to adjust the code according to your specific requirements and test it to ensure it works as expected.
Rate this post
4 of 5 based on 8935 votesComments