Question

I'm trying to get posts that have either of the tags with specific ID's assigned (18 and 23). I struggle however to apply offset to it. offset setting seems to apply only to posts already filtered. It seems it first selects all posts with 18 or 23 tag and then, out of this list applies offset to it and returns only posts after the offset number.

Is there any way to apply offset first to all the posts and then from this point select all the posts with specific tags? My code is below

$args = array(
                        'posts_per_page'   => 50,
                        'offset'           => 10, //this doesn't work
                        'category'         => '',
                        'orderby'          => 'post_date',
                        'order'            => 'DESC',
                        'include'          => '',
                        'exclude'          => '',
                        'meta_key'         => '',
                        'tag__in'      => array('23','18'),
                        'meta_value'       => '',
                        'post_type'        => 'post',
                        'post_mime_type'   => '',
                        'post_parent'      => '',
                        'post_status'      => 'publish',
                        'suppress_filters' => true );


 $the_query = new WP_Query( $args );
Was it helpful?

Solution

I'm not sure if I understand the question correctly, but if you want to use the offset for all posts (not just the ones that are tagged with term 18 or 23), I'm thinking you could use a sub query:

// the sub query will select a maximum of 50 posts starting from the supplied $offset
// notice that it will retrieve max 50 posts disregarding associated tags
// you may want to move the 'ORDER BY' clause into the sub query as well
$query = "SELECT p.* FROM (
            SELECT * FROM wp_posts 
            ORDER BY post_date 
            WHERE post_type = 'post' && post_status = 'publish' 
            LIMIT $offset, 50
          ) p 
          INNER JOIN wp_term_relationships tr ON tr.object_id = p.id && tr.taxonomy_term_id IN (23, 18)
          INNER JOIN wp_terms t ON t.term_id = tr.taxonomy_term_id
          ORDER BY p.post_date DESC";

$the_query = new WP_Query($query);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top