• Resolved Matías

    (@m4tl4s)


    Hi again, Héctor:

    I’m currently using this to collect views in different period times, but the other day I tried to order posts using this code and it doesn’t work:

    <?php $popular_posts = new WPP_Query( array('range' => 'last24hours', 'order_by' => 'views', 'limit' => 8) ); $popular_days_ago = "$popular_days days ago"; $recent = new WP_Query(array( 'ignore_sticky_posts'=> 1, 'date_query' => array( array( 'after' => $popular_days_ago )) )); while($recent->have_posts()) : $recent->the_post(); ?>

    • This topic was modified 4 years, 5 months ago by Matías.
    • This topic was modified 4 years, 5 months ago by Matías.
    • This topic was modified 4 years, 5 months ago by Matías.
    • This topic was modified 4 years, 5 months ago by Matías.
Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author Hector Cabrera

    (@hcabrera)

    Hi Matías,

    I have some questions for you:

    1. You’re instantiating a WPP_Query object ($popular_posts) which you don’t use in your code. Why? (By the way that class was deprecated recently, please check the changelog.)

    2. What is $popular_days?

    3. You say you’re using the code from here to store views count in meta fields, yet you’re not using them in your code?

    4. Taking all of the above into consideration, what exactly do you expect your code to do?

    Thread Starter Matías

    (@m4tl4s)

    1. I also tried with \WordPressPopularPosts\Query but apparently the entire code is wrong.

    2. $popular_days is a function of my theme to sort popular posts by the date published, for example the most viewed posts published 1, 2, 3…7…30, etc. days ago. But without that line it’s the same!

    3. Actually I’m using it right now but with other meta key name and works well:

    <?php global $post; $mvp_related_num = get_option('mvp_related_num'); $pop_days = esc_html(get_option('mvp_pop_days')); $popular_days_ago = "$pop_days days ago"; $category = get_the_category(); $current_cat = $category[0]->cat_ID; $recent = new WP_Query(array('cat' => $current_cat, 'posts_per_page' => $mvp_related_num, 'ignore_sticky_posts'=> 1, 'post__not_in' => array( $post->ID ), 'orderby' => 'meta_value_num', 'order' => 'DESC', 'meta_key' => 'post_views_day', 'date_query' => array( array( 'after' => $popular_days_ago )) )); while($recent->have_posts()) : $recent->the_post(); ?>

    4. The same but without saving the views data in post meta.

    • This reply was modified 4 years, 5 months ago by Matías.
    • This reply was modified 4 years, 5 months ago by Matías.
    • This reply was modified 4 years, 5 months ago by Matías.
    • This reply was modified 4 years, 5 months ago by Matías.
    • This reply was modified 4 years, 5 months ago by Matías.
    Plugin Author Hector Cabrera

    (@hcabrera)

    I also tried with \WordPressPopularPosts\Query but the code is wrong.

    That didn’t answer my question from earlier. You’re declaring a variable that you’re not using in your code. How do you expect it to do anything if you don’t use it at all?

    $popular_days is a function of my theme to sort popular posts by the date published, for example the most viewed posts published 1, 2, 3…7…30, etc. days ago. But without that line it’s the same!

    Actually I’m using it right now with another meta key name and works well

    I’m not familiar with the theme you’re using (you didn’t even mention which one it is so I couldn’t test that code if I wanted to) so it’s best if you asked the developer of your theme directly how to implement the views meta fields into the code so that it sorts posts by views count (which I assume is what you’re trying to do here.)

    Thread Starter Matías

    (@m4tl4s)

    That didn’t answer my question from earlier. You’re declaring a variable that you’re not using in your code. How do you expect it to do anything if you don’t use it at all?

    Sorry but I’ll respond this in spanish to clearly say what I want to say… Usé eso únicamente para probar sin saber realmente si funcionaría… Ahora sé qué falta algo en el código para que funcione y es lo que no sé qué es. ¿Qué falta?

    Lo que quiero es simplemente mostrar los posts más populares (igual que hasta ahora) tomando la data de WPP directamente y sin guardar la data de los views de cada período en post meta una y otra vez, porque provoca una sobrecarga en mi hosting, o algo así me dijo mi proveedor.

    Before, I used another plugin and I could mix the code. Look especially since “orderby”:

    <?php global $post; $mvp_related_num = get_option('mvp_related_num'); $pop_days = esc_html(get_option('mvp_pop_days')); $popular_days_ago = "$pop_days days ago"; $category = get_the_category(); $current_cat = $category[0]->cat_ID; $recent = new WP_Query(array('cat' => $current_cat, 'posts_per_page' => $mvp_related_num, 'ignore_sticky_posts'=> 1, 'post__not_in' => array( $post->ID ),'order' => 'DESC', 
    
    'orderby' => 'post_views', 'views_query' => array(
    	  'year'  => date( 'Y' ), // this year
    	  'month' => date( 'm' ), // this month
    	  'day'   => date( 'd' ), // this day
    	   ),
    
    'date_query' => array( array( 'after' => $popular_days_ago )) )); while($recent->have_posts()) : $recent->the_post(); ?>

    That’s all! I hope you understand!

    • This reply was modified 4 years, 5 months ago by Matías.
    • This reply was modified 4 years, 5 months ago by Matías.
    • This reply was modified 4 years, 5 months ago by Matías.
    Plugin Author Hector Cabrera

    (@hcabrera)

    Hi Matías, good day!

    Lo que quiero es simplemente mostrar los posts más populares (igual que hasta ahora) tomando la data de WPP directamente y sin guardar la data de los views de cada período en post meta (…)

    That can’t be done: if you want to use the core WP_Query class to list posts, WordPress won’t know how to sort posts by views unless it uses the views post meta.

    If you’re having performance issues on your site:

    1. Follow the suggestion posted here (step one) to avoid writing views meta to the database too often.
    2. Consider using either of the performance suggestions/solutions available to make sure WPP doesn’t slow down your website.

    Edit: do take into account the comment I left on that topic or else the code won’t work properly.

    • This reply was modified 4 years, 5 months ago by Hector Cabrera. Reason: Added important notice
    Plugin Author Hector Cabrera

    (@hcabrera)

    Marking as resolved due to inactivity.

    Thread Starter Matías

    (@m4tl4s)

    Thank you! I checked again and WPP isn’t the one causing performances issues so I better won’t use “accuracy”.

    Y una última pregunta para no abrir otro tema… estoy algo confundido con la opción “Límite del registro”. Si decido guardar datos por 30 días por ejemplo, se limpiará solo la tabla “popularpostssummary”? Y la tabla “popularpostsdata” quedará intacta?

    Plugin Author Hector Cabrera

    (@hcabrera)

    Si decido guardar datos por 30 días por ejemplo, se limpiará solo la tabla “popularpostssummary”? Y la tabla “popularpostsdata” quedará intacta?

    Yes and yes.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘WPP Query doesn’t work’ is closed to new replies.