0

but to be specific, i need to retrieve certain data from the post - and showed one category's post datas first (preference) in the coupon's list and then other categories post data second in the list.

I meant to say that suppose first 6 coupons are first preference, then it will be shown first then the rest. And then goes the pagination. Therefore as, @Swopnil Dangol suggested, i try to use the 2 LOOPS for (one category & rest of the category respectively.

But the problem is even i click the second page, it is showing the first page data, not the rest of the datas. Thanx in advance. I try to find answers through the internet no success, plz help.

<?php 
            $args = array();
            $args['post_type'] = 'coupons';
            $args['cat'] = 20;
            $query1 = new WP_Query($args);      

            while($query1->have_posts() ) : $query1->the_post();
            $thumb = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
            $discount = get_post_meta($post->ID, '_single_discount', true);
            $date = get_post_meta($post->ID, '_single_date', true);
            $code = get_post_meta($post->ID, '_single_code', true);
            $url = get_post_meta($post->ID, '_single_url', true);   
            $daysleft = round( ($date-time()) / 24 / 60 / 60);  
        ?>
        <div class="coupon">
            <?php if($thumb != '') { ?>
                <div class="coupon-top">
                    <figure>
                        <a href="<?php the_permalink();?>"><img src="<?php echo aq_resize($thumb, 300, 200, true); ?>" alt="<?php the_title();?>"></a>
                    </figure>
                    <div class="coupon-title">
                        <a href="<?php the_permalink();?>"><?php truncate_title(15);?></a>
                    </div>
                </div>
            <?php } ?>
            <div class="coupon-bottom">
                <div class="coupon-offer">
                    <?php echo  substr($discount, 0, 15);?>
                </div>
                <div class="coupon-date">
                    <?php
                        if($date == '')_e('Хүчинтэй', 'Couponize');
                        else if($daysleft <= 0) _e('Дууссан', 'Couponize'); 
                        else echo sprintf( _n('%d day left.', '%d Хоног Үлдсэн.', $daysleft, 'Couponize'), $daysleft ); 
                    ?>
                </div>
            </div>
        </div>
        <?php endwhile;  wp_reset_postdata();?>


        <?php 
            $args = array( 'category__not_in' => array(20));
            $args['post_type'] = 'coupons';
            $query2 = new WP_Query($args);

            while($query2->have_posts() ) : $query2->the_post();
            $thumb = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
            $discount = get_post_meta($post->ID, '_single_discount', true);
            $date = get_post_meta($post->ID, '_single_date', true);
            $code = get_post_meta($post->ID, '_single_code', true);
            $url = get_post_meta($post->ID, '_single_url', true);   
            $daysleft = round( ($date-time()) / 24 / 60 / 60);  
        ?>
        <div class="coupon">
            <?php if($thumb != '') { ?>
                <div class="coupon-top">
                    <figure>
                        <a href="<?php the_permalink();?>"><img src="<?php echo aq_resize($thumb, 300, 200, true); ?>" alt="<?php the_title();?>"></a>
                    </figure>
                    <div class="coupon-title">
                        <a href="<?php the_permalink();?>"><?php truncate_title(15);?></a>
                    </div>
                </div>
            <?php } ?>
            <div class="coupon-bottom">
                <div class="coupon-offer">
                    <?php echo  substr($discount, 0, 15);?>
                </div>
                <div class="coupon-date">
                    <?php
                        if($date == '')_e('Хүчинтэй', 'Couponize');
                        else if($daysleft <= 0) _e('Дууссан', 'Couponize'); 
                        else echo sprintf( _n('%d day left.', '%d Хоног Үлдсэн.', $daysleft, 'Couponize'), $daysleft ); 
                    ?>
                </div>
            </div>
        </div>
        <?php endwhile; wp_reset_postdata();?>

enter image description here

1 Answer 1

0

You could use 2 different loops: 1 for some of your categories and another for the rest.

//Here 2,6 are the category ids
$query1 = new WP_Query( array( 'category__in' => array( 2, 6 ) ) );
// The Loop
while ( $query1->have_posts() ) {
    $query1->the_post();
}
// Restore original Post Data
wp_reset_postdata();

$query2 = new WP_Query( array( 'category__not_in' => array( 2, 6 ) ) );
// The Loop
while ( $query2->have_posts() ) {
    $query2->the_post();
}
// Restore original Post Data
wp_reset_postdata();

Use this code on the page where you would like to show your category posts. If your're new to this,maybe you should first know about template hierarchy to figure out which page is being loaded and in which file you should place your code.

3
  • thanx for the suggestion, can you kindly tell me where i should put this statement? thanx in advance, plz bear with me, i am new to this. Commented May 11, 2016 at 13:02
  • Ive edited the answer, you can check it out Commented May 11, 2016 at 17:48
  • hi thanx for the suggestion. I used two different loops and it is not showing the expected result, can you kindly tell me where I am missing, plz refer to more detailed info from the below answer. thanks again. can you plz look through the code and where i am missing. Commented May 12, 2016 at 5:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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