1

Im successfully filtering my Porducts by a meta_key and its value. I display (query) the result in a custom page template. The query includes also a Pagination. Everything works fine - BUT sometimes after 3 to 5 page refreshes the query outputs some products twice - especially after calling the next page.

I figured out that the problem is caused by the 'orderby' => 'meta_value_num' part of the following code. Any idea why this happens and any idea how to solve this problem?

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

$args = array(
    'meta_key' => '_recoed',
    'meta_compare' => '>',
    'meta_value' => '0',
    'post_type' => 'product',           
    'orderby' => 'meta_value_num',
    'order' => 'DESC',
    'paged' => $paged
);

query_posts($args);

get_template_part('index', 'most-liked-products');

The Part in my Page template looks like

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

<?php if (isset($_GET['pnum']) && $_GET['pnum'] > 1) { $paged = 2; } //stop ads from repeating ?>           

<?php
    get_template_part('index-masonry-inc');
    endwhile;
    else :
?>
....

<?php endif; ?>
evavienna
  • 1,059
  • 11
  • 29

1 Answers1

0

I solved the Problem by using 'orderby' => 'meta_value meta_value_num', instead of 'orderby' => 'meta_value_num', after realizing that the the 'orderby' => ' argument can take more then one parameter! Hope this helps others too. (More about the WP Query)

Side Note: Maybe one of you can tell us why this is more precise than only one parameter. Feel free to comment below. thx

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

$args = array(
    'meta_key' => '_recoed',
    'meta_compare' => '>',
    'meta_value' => '0',
    'post_type' => 'product',           
    'orderby' => 'meta_value meta_value_num', // The OrderBy argument can take more then one parameter
    'order' => 'DESC',
    'paged' => $paged
);

query_posts($args);

get_template_part('index', 'most-liked-products');
evavienna
  • 1,059
  • 11
  • 29