Hot answers tagged

15

The offset index for WP_Query generally works with pagination. When you set pagination to a -1 the function assumes you're getting all posts and there will be no pagination or offset. So to counteract this you would set the posts_per_page to a high number like 999. Reading the [Function Reference on WP_Query][enter link description here]1 the pagination ...


9

Case #1: Simple Offset You want to 'offset' posts of a category archive by 'n', i.e. you simply don't want to show the first/latest 'n' posts in an archive. That is, (considering the posts_per_page setting in WP Dashboard > Settings > Reading is set to 10) you want posts 11 to 20 to be shown on the first page (e.g. example.com/category/tech/), 21 to 30 on ...


7

This is quite an interesting question (which I have upvoted, specially for your approach and research). The big curveball here is the first page of the query: You cannot set the query to return 0 posts on the first page By moving the page content of every page up by one page, you will loose the last page as the query will still only have the same amount of ...


5

get_term_by returns an object, try passing $term->slug to your query instead.


5

You need to get the post ids from the original main query and exclude them. Then you should remove your offset from your custom query. That should do the trick. Random ordering basically ignores the offset parameter, so you need to explicitely remove the posts from the query to exclude them You can use wp_list_pluck() to get an array of post ids from the ...


3

Here is a concept that you can try out to exclude 5 posts per category each from category 3 and 5 where posts are excluded that belongs to category 9 and either of the before mentioned categories. Here is the function: (CAVEAT: Requires PHP 5.4+ as I used new short array syntax. Change as needed) /** * function get_sticky_category_posts() * * Get an ...


3

You might try this kind of pre_get_posts hook approach (untested): add_action( 'pre_get_posts', function( $q ) { $exclude_cats = [ 12, 34 ]; // <-- Edit these category ids! if( ! is_admin() && $q->is_main_query() && ! is_singular() && ! is_category( $exclude_cats ) ) { $...


3

I can't use only one Loop because of how complicated my markup is. Markup, no matter how complicated, should never ever be a valid basis when coming to deciding to run multiple loops. Multiple loops should only ever be used if there is absolutely no other means to achieve a certain result, and that is almost always just when different results are needed per ...


3

You are going about this the hard way, and the less efficient way. You already have a Loop on the page. You should be able to use that and that alone. if (have_posts()) { while (have_posts()) { the_post(); if (6 < $wp_query->current_post) { // formatting for your first six posts } else { // formatting for the other posts }...


3

You're 95% of the way there already. Just add if ( $my_query->have_posts() ) and incorporate your $odd variable: <?php $my_query = new WP_Query('offset=5&showposts=10'); if ( $my_query->have_posts() ) : $odd = false; while ($my_query->have_posts()) : $my_query->the_post(); $odd = !$odd; ?> <div class="&...


2

This will most likely require two loops to the best of my knowledge. The second loop just needs to know to exclude the post you just queried. Something like the following should do the trick. $first_id = 5; // This should be set in the previous loop to the post ID of the post returned by your first query. $args2 = array( 'post_type' => 'portfolio', ...


2

You're looking in WordPress core files for the cause of the PHP notice, which is a waste of time, as 1) I doubt you've found a new bug in WordPress, and 2) you don't want to modify WordPress core files to fix a theme or plugin issue, and 3) the error is caused by a theme or plugin and not WordPress core, but shows up in the PHP notice as pointing to core ...


2

doing 3 separate queries is unnecessary and inefficient, do one query for all 12 posts and output your container markup every fourth post. $args = array( 'tag__in' => $tag_ids, 'post__not_in' => array($post->ID), 'posts_per_page'=> 12 ); $my_query = new WP_Query( $args ); if( $my_query->have_posts() ): ?> <div class="...


2

If you only wish to effect the one, single query then just can pass the offset argument through the arguments array. $offset = 1; $ppp = 3; if ( $query->is_paged ) { $page_offset = $offset + ( ($query->query_vars['paged']-1) * $ppp ); } blog_items = array( 'post_type'=> 'post', 'paged' => $paged, 'posts_per_page'=> $ppp, ...


2

To have the pagination work properly you need to filter the posts at the WordPress main query level using the pre_get_posts action. By looking at your code, I don't see where the post specific information is involved. Is $course_options holding information for the post?


2

I messed around with the code some more, and my particular issue actually ended up relating to the original if statement parameters. Changing the condition to check the home page for queries to if ( $query->is_home() && ! $query->is_main_query() ) { return; } and else { //This is the first page. Just use the offset... if ( $...


2

The order doesn't matter. That query will get 7 posts from the "events" category, skipping over the 5 most recent (since the default order is by date).


1

It helps to write problems down. After I haven't used pre_get_posts extensively, I focused too much on that area for the cause, but it was the if clause. Changed to if ( ! is_admin() && $query->is_main_query() && ! is_post_type_archive( 'cpt' ) ) { return $query; } elseif ( ! is_admin() && $query->is_main_query() &&...


1

The "offset" depends on the query-- sort order, conditions like category or tag, meta queries, etc.--, and it changes every time a post is added to the blog. It isn't something you "get" so much as something you calculate. On a "single post" page the query is pretty straightforward and returns a single post-- that is, 0 offset. Assuming what you want is ...


1

I don't think that you can get an offset of the page outside of the loop, since it will be out of the context. Offset may vary depending on the arguments you are using to get posts for the loop. Easiest solution will be to calculate offset in the app. You will need to pass post_id as an attribute and then loop through all the posts obtained by ?json=...


1

I don't have enough stackexchange points to comment on the above, but I do have a correction. Case #1: Simple Offset worked for my site very well. It just needs a fix so that it returns the default offset when the special condition isn't met: function itsme_adjust_category_offset_pagination( $found_posts, $query ) { $offset = 10; if( $query->...


1

The problem of your code is that $wp_query is a global variable. Once the sidebar is loaded via a function (dynamic_sidebar) you need to globalize that variable before use. However, once you are in the single view the current_post is always 0. You have to run another query, loop it, and check the every post id in this query against the original queried post ...


1

I couldn't understand your question. exactly Do you want next, prev post pagination? or You want to see next and prev posts titles or other content? Actually you placed an incorrect value to offset. You have to do something like this. // Initialize where to start the post from, 0 is most recent post $init_count = 0; // Get the current page integer $page ...


1

Here's your code wrapped up in a plugin. The important thing is that it prepare()s the input, so the query is safe. You can define 2 arguments: Offset & Limit. Those are basically just the SQL LIMIT. If you don't have any use for the plugin, just deactivate it, as it won't do anything - its output is attached to a filter … $most_commented = ...


1

I think your searching for this solution http://codex.wordpress.org/Making_Custom_Queries_using_Offset_and_Pagination The Problem Offsets are useful because they can allow a developer to skip a certain number of WordPress posts before starting output. Unfortunately, many developers find out that hard way that setting an offset value in their custom ...


1

i use this code in my portfolio http://pocketapps.co/ <?php $args = array( //your argument code ); query_posts($args);?> <ul> <?php $ls=0; while ( have_posts() ) : the_post(); ?> ...


1

Fetch all five posts in one query, store the result in a variable and take one each time you want to show an advertisement. Now you cannot get duplicates, and more important: you save four queries.


1

Yep - it is pretty inefficient. Here is a rewrite: <div id="engagement"> <?php $images = new WP_Query( array( 'post_parent' => get_the_ID(), 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', '...


1

Reason of this behavior is pretty simple. First of all you have to know that get_posts uses WP_Query to get posts. So let's look at WP_Query implementation. On line 1998 of query.php you can find: if ( $q['posts_per_page'] == -1 ) { $q['nopaging'] = true; Then on line 2544 of query.php you find: if ( empty($q['nopaging']) && !$this->...


1

Neither of those are complete loops. That is, in both you have the start of an if but not the end of it, and the start of a while loop, but no the end of it. Also, you shouldn't be using query_posts either, as it over writes the main query and is rarely the right function for pulling posts. get_posts would be better. I would do this, using one as an example:...


Only top voted, non community-wiki answers of a minimum length are eligible