3

I have a Query searching for a meta value I have added to my posts (meta-select). I have it feeding out in a loop with 4 results. Let's say I want this loop to start with the meta value "2" and work its way up "3","4","5" each time adding 1 to the value in the loop. Here is my current code which obviously just queries for the value "2" where do I add an increment in this function?

<?php 

// args
$args = array(
    'posts_per_page'   => 4,
    'post_type' => 'post',
    'meta_key' => 'meta-select',
    'meta_value' => '2'
);

// get results
$feature_query = new WP_Query( $args );

// The Loop
?>
<?php if( $feature_query->have_posts() ): ?>
    <ul>
    <?php while ( $feature_query->have_posts() ) : $feature_query->the_post(); ?>
        <li>
            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
        </li>
    <?php endwhile; ?>
    </ul>
<?php endif; ?>

<?php wp_reset_query();?>
  • I'm confused, are you trying to order your posts by a meta value in ascending order? Or do you just want an incrementer each time it loops through a post? Or are you trying to get 4 posts each of each meta_value, assuming you have a meta_value of 2, 3, 4, etc. – Howdy_McGee Aug 21 '14 at 21:44
  • I want it to increment in ascending order starting from 2 and ending at 5. so it would return the post with meta-select=2 then meta-select=3 then meta-select=4 then meta-select=5 and then end. Does that make sense? Thank you for the help! – bmlbml Aug 21 '14 at 21:52
  • Yeah that makes sense, do you want 1 post from each I'm assuming, so if there's 10 posts under meta-select-2 you want the first post out of the 10 correct? – Howdy_McGee Aug 21 '14 at 21:56
  • yeah. just the first. I also have the meta set to not allow for 2 posts to have the same value so It shouldn't matter I think. But otherwise the first. – bmlbml Aug 21 '14 at 22:10
  • Sorry, just edits to 'posts_per_page' => 4, I think that is right. – bmlbml Aug 21 '14 at 23:39
0

Not tested but you can try this:

$args = array(
    'posts_per_page'   => 4,
    'post_type'        => 'post',
    'meta_key'         => 'meta-select',    
    'order'            => 'ASC',
    'orderby'          => 'meta_value_num',
    'meta_query' => array(
        array(
          'key'     => 'meta-select',
          'value'   => array( 2, 3, 4, 5 ),
          'compare' => 'IN',
        ),
      ),
);

Ordering is done by value of meta field meta-select in ascending order. Also in meta_query, meta value is checked in the array of possible values

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.