0

This is my code, why cant i get post per page and offset result ?.

<?php 
    $myquery['tax_query'] = array(
        'relation' => 'OR',
        'posts_per_page' => 3,
        'offset' => 3,
    array(
        'taxonomy' => 'brands',
        'terms' => array('iHOME'),
        'field' => 'slug',
        ));
    query_posts($myquery);?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_post_thumbnail();?>
<b><?php the_title();?></b>
<p><?php echo excerpt(25); ?></p>
<?php echo esc_html( get_post_meta( get_the_ID(), 'category', true ) ); ?>

<?php endwhile; else: ?>
<?php endif; ?>
2
  • it's <?php not >?php
    – JMau
    Jun 30, 2013 at 12:50
  • @ShihabMalayil here is your solution: MARKDOWN HELP. Please, get into a habit to write your questions nicely. Jun 30, 2013 at 13:06

1 Answer 1

2

Your query args are messed up. Posts per page and offset params are not part of the tax_query array. Check out the example in the Codex:

$args = array( 
    'post_type' => array('your_cpt'),
    'posts_per_page' => 3,
    'offset' => 3,
    'tax_query' => array(
       'relation' => 'OR',
        array(
            'taxonomy' => 'brands',
            'terms' => array('iHOME'),
            'field' => 'slug',
            )
    ) 
);

$tax_query = new WP_Query($args);

if ( $tax_query->have_posts() ) : while ( $tax_query->have_posts() ) : $tax_query->the_post(); 

    the_post_thumbnail();
    the_title();
    echo excerpt(25); 
    echo esc_html( get_post_meta( get_the_ID(), 'category', true ) ); 

endwhile; else: 
endif; 

wp_reset_postdata();

Also, I couldn't really explain why, but I always see it written around here to avoid using query_posts, so I replaced that in my answer with new WP_Query.

One more thing, if you are querying for a post type other than posts, you'll need to define the post_type query parameter.

8
  • @helgatheviking- You are correct. 'tax_query' => array( . thank you very much. expecting more....! Jun 30, 2013 at 14:11
  • What are you expecting more of? The code should work now. Jun 30, 2013 at 14:14
  • iam sorry, i mean help me next time. thank you. tax_query' => array( ,you just missed =>. Jun 30, 2013 at 14:16
  • We'll see. I'm only on here some of the time. Fixed the typo. Don't forget to mark as resolved if this solves your question. Jun 30, 2013 at 14:25
  • sure, will do... Jun 30, 2013 at 14:44

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.