• Resolved jenseo

    (@jenseo)


    Hi!

    First of all, thanks for a great plugin! I posted this question on the github page, but then I realised that the plugin was listed here again, so I decided to post it here as well. I guess it’s the more appropriate place.

    This is my code to fetch child posts inside a single post template, which works, except for one thing:

    <?php
    $initial_count = get_post_meta($post->ID, '_initial_count', true);
    $images = get_children('post_parent='.$post->ID.'&post_status=publish&post_type=user_images');
    if ( ! empty( $images ) ) {
        $image_ids = wp_list_pluck( $images, 'ID' );
        $image_ids = implode(',',$image_ids);
     echo do_shortcode('[ajax_load_more post_type="user_images" preloaded="true" preloaded_amount="'.$initial_count.'" meta_key="_point" post__in="'.$image_ids.'" orderby="meta_value_num" posts_per_page="20" pause="true" scroll="false" button_label="Ladda fler bilder..." transition="fade" container_type="div" css_classes="user-images"]');
    }
    wp_reset_postdata();
    ?>

    Users can vote for posts on my site, and what I’m trying to do is to order the posts by the meta key “_point” (the number in this custom field).

    It works for the preloaded posts, but when I load more, sometimes it displays some of the same posts as in the preloaded list.

    I think there might be two possible explanations for this:

    It could be a cache related issue, although I don’t think that would be the case.

    or…

    It could be that I also need to specify that it should posts with the same number of votes by publish date/time. Something like this:

    $query->set('orderby', array('meta_value_num' => 'DESC', 'date' => 'DESC'));
    Would this be possible?

    // Jens.

    https://wordpress.org/plugins/ajax-load-more/

Viewing 15 replies - 1 through 15 (of 21 total)
  • Plugin Author Darren Cooney

    (@dcooney)

    Hi Jens,
    Interesting problem.

    The Preloaded and Standard ALM query accept the same properties so im not sure why it would duplicate an entry.

    • Is your site high traffic?
    • Is it possible the votes are changing as you load more posts?
    • When do the posts duplicate? Is it on the first ajax query?

    Where would you set $query->set('orderby', array('meta_value_num' => 'DESC', 'date' => 'DESC')); ?

    Thread Starter jenseo

    (@jenseo)

    Hi,
    thanks for the quick reply!

    It is high traffic, but I have checked if the votes have changed, which they have not. They duplicate on the first ajax request yes. The preloaded posts order correctly, but not the remaining ones.

    The posts in the list are published with one minute in between, just to make sure they not have the exact same time.

    In the current query, the _point is always set, either with 0 or a number of votes.

    The query I mentioned have been used when I have had modified pre_get_posts on posts with a custom field that’s only set on some posts. It looks like this:

    add_filter( 'posts_where', 'wpq_posts_where_archive', PHP_INT_MAX );
    
    		$query->set( 'post_type', array( 'open_list', 'post' )  );
    
    		$query->set( 'meta_query', array(
    			'relation' => 'OR',
    			array(
    				'key'     => '_views_today',
    				'compare' => 'NOT EXISTS',
    			),
    			array(
    				'key'     => '_views_today',
    				'compare'   => 'EXISTS',
    			),
    		));
    		$query->set('orderby', array('meta_value_num' => 'DESC', 'date' => 'DESC'));

    Could there be something wrong with how I have used the shortcode? To be honest, I haven’t fully been able to wrap my head around the query part of it.

    A few possible issues I can think of:

    • Is it ok to have a certain amount of preloaded posts, and another amount of posts per page? This is what I do in my case.
    • Do I need to set an offset of the same amount of posts as the preloaded ones?
    • Is it ok to use it without a meta_value as in my case? Since the values are all different?

    Thanks!

    // Jens.

    Thread Starter jenseo

    (@jenseo)

    Extra info: Posts can have the same amount of votes, but I guess they should be ordered by date then?

    Did you tried to insert offset value in shortcode?

    Plugin Author Darren Cooney

    (@dcooney)

    jenseo wouldn’t need to offset the shortcode because the Preloaded addon takes care of that.

    Jenseo – I don’t see a meta_value in your shortcode. Is that left out on purpose?

    Thread Starter jenseo

    (@jenseo)

    Hey!
    I haven’t added meta key because there isn’t a fixed number, but maybe I have missunderstood this parameter? Should i add something like meta_key=number or something like that?

    Plugin Author Darren Cooney

    (@dcooney)

    meta_key only gets rendered if meta_value is also present.

    an example would be [ajax_load_more meta_key="animal" meta_value="elephant"]

    Thread Starter jenseo

    (@jenseo)

    Ok, so what should be added to key and value when I have a custom field called “_point” and different numbers stored on each post?

    Plugin Author Darren Cooney

    (@dcooney)

    Ok sorry.
    I’m wrong about what I said. meta_value is not required.

    Can you post your complete page template?

    Thread Starter jenseo

    (@jenseo)

    <?php get_header(); ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    	<div id="content-single">
    	<div id="single-post-wrapper">
    	<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
    					<!-- google_ad_section_end -->
    					<!-- google_ad_section_start -->
    					 <div class="meta-wrapper-large clearfix"><?php if ( function_exists('yoast_breadcrumb') ) {
    yoast_breadcrumb('<p id="breadcrumbs">','</p>');
    } ?>
    </div>
    
    <h1 class="entry-title"><?php the_title(); ?></h1>
    
    <div class="entry">
    
    <?php the_content(); ?>
    
    <?php
    $initial_count = get_post_meta($post->ID, '_initial_count', true);
    $images = get_children('post_parent='.$post->ID.'&post_status=publish&post_type=user_images');
    if ( ! empty( $images ) ) {
        $image_ids = wp_list_pluck( $images, 'ID' );
        $image_ids = implode(',',$image_ids);
     echo do_shortcode('[ajax_load_more post_type="user_images" preloaded="true" preloaded_amount="'.$initial_count.'" meta_key="_point" post__in="'.$image_ids.'" orderby="meta_value_num" order="DESC" posts_per_page="20" pause="true" scroll="false" button_label="Ladda fler bilder..." transition="fade" container_type="div" css_classes="user-images"]');
    }
    wp_reset_postdata();
    ?>
    
    	<?php endwhile; endif; ?>
    	<?php get_sidebar('sidebar'); ?>
    	</div>
    	</div>
    	</div>
    
    	<?php get_footer(); ?>
    Thread Starter jenseo

    (@jenseo)

    Could it be an issue with wp_reset_postdata?

    Plugin Author Darren Cooney

    (@dcooney)

    Can you try this?
    I think the issue is the your opening loop.

    <?php get_header(); ?>
    
    <div id="content-single">
       <div id="single-post-wrapper">
          <div <?php post_class() ?> id="post-<?php the_ID(); ?>">
          <!-- google_ad_section_end -->
          <!-- google_ad_section_start -->
          <div class="meta-wrapper-large clearfix"><?php if ( function_exists('yoast_breadcrumb') ) {
          yoast_breadcrumb('<p id="breadcrumbs">','</p>');
          } ?>
          </div>
    
          <h1 class="entry-title"><?php the_title(); ?></h1>
    
          <div class="entry">
          <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
          <?php the_content(); ?>
    
          <?php endwhile; endif; ?>
          <?php
          $initial_count = get_post_meta($post->ID, '_initial_count', true);
          $images = get_children('post_parent='.$post->ID.'&post_status=publish&post_type=user_images');
          if ( ! empty( $images ) ) {
             $image_ids = wp_list_pluck( $images, 'ID' );
          $image_ids = implode(',',$image_ids);
             echo do_shortcode('[ajax_load_more post_type="user_images" preloaded="true" preloaded_amount="'.$initial_count.'" meta_key="_point" post__in="'.$image_ids.'" orderby="meta_value_num" order="DESC" posts_per_page="20" pause="true" scroll="false" button_label="Ladda fler bilder..." transition="fade" container_type="div" css_classes="user-images"]');
          }
             wp_reset_postdata();
          ?>
    
          <?php get_sidebar('sidebar'); ?>
          </div>
       </div>
    </div>
    
    <?php get_footer(); ?>

    Thread Starter jenseo

    (@jenseo)

    Thanks! I think moving the endwhile; endif; statement up to before the load more query does the trick! I just have to make sure it won’t effect anything after the list, but I think it will be ok!

    Awesome, thanks a lot!

    Plugin Author Darren Cooney

    (@dcooney)

    No problem.
    I should have asked sooner for the template code.

    Thread Starter jenseo

    (@jenseo)

    Hey, no worries! I should have thought of moving the statement sooner as well 😉

    Thanks for a great plugin and great support, will give you a five star rating after I have finished writing this 😉

Viewing 15 replies - 1 through 15 (of 21 total)

The topic ‘Query by meta_value_num and date?’ is closed to new replies.