Viewing 6 replies - 1 through 6 (of 6 total)
  • Put your if function_exists loops inside your post template.
    Put your add_action inside your functions.php in your child theme.

    You also should be connecting to your db and running the queries against your _post and _metapost tables to make sure you are retrieving the data you expect.
    https://ben.lobaugh.net/blog/45390/wordpress-how-to-view-the-sql-query-generated-by-wp_query

    Thread Starter chaimsem

    (@chaimsem)

    ok so just to be clear,

    put this:

    if( function_exists( 'stats_get_csv' ) ) {
        $top_posts = stats_get_csv( 'postviews', 'period=month&limit=30' );
    }

    inside the post template, even though jetpack already exists and runs this action as a plugin?

    another question is, I am unclear of what to put here:

    // Posts or Portfolio Widget
    add_action( 'elementor/query/my_custom_filter', function( $query ) {
       ---- HERE----- // Modify the posts query here  ----- HERE
    } );

    based on the link, it sounds like something like this:

    // Posts or Portfolio Widget
    add_action( 'elementor/query/my_custom_filter', function( $query ) {
        $query = array(
        [0] => array(
            ['post_id'] => 0
            ['post_title'] => 'Home page'
            ['post_permalink'] => 'https://www.example.com/'
            ['views'] => 6806
        )
        [1] => array(
            ['post_id'] => 8005
            ['post_title'] => 'Hello World!'
            ['post_permalink'] => 
            ['views'] => 1845
        )           
        /* till item [29] */
    } );

    not sure if this would be accurate?

    Thanks a lot for the reply!

    • This reply was modified 3 years, 11 months ago by chaimsem.
    • This reply was modified 3 years, 11 months ago by chaimsem.

    oh, sorry I didn’t actually realize you didn’t have the code already written.
    I was just outlining where to put ‘actions’ and ‘conditional loops’ in general within the WordPress hierarchy.
    Before we look at your code – why not just use a plugin? re-inventing the wheel sucks.

    You could add a custom field to your posts, and use the Dynamic OrderBy option from here:
    https://www.ads-software.com/plugins/advanced-post-queries/
    or there is this one :
    https://github.com/cabrerahector/wordpress-popular-posts
    https://www.ads-software.com/plugins/wordpress-popular-posts/

    I dunno about Jetpack.
    It’s always sucked in my experience, so never have it installed.

    Thread Starter chaimsem

    (@chaimsem)

    thanks so much!

    I did try the advanced post queries and it didn’t have the function I am looking for.

    The wordpress popular plugin does what I am looking for but it is not compatible with wordpress.com hosting.

    It happens to be that the jetpack plugin is not my go to option in general but when it comes to top posts, they are doing a great job, also for related posts in my opinion.

    The query ID gets generated by the elemnetor plugin which is the elementor/query/my_custom_filter my_custom_filter and the rest has to be built by me which is the query itself.

    What I am trying to do is to query the existing Jetpack top posts query into the elementor post sort “widget”.

    Thanks!

    Oh, sorry
    I didn’t realize you were running a site on wordpress.com

    Support for that is here :
    https://wordpress.com/support/contact/

    Thread Starter chaimsem

    (@chaimsem)

    Thanks for you time!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Sorting by most views/popular posts in WordPress’ is closed to new replies.