I'm building a Wordpress page which aggregates all posts of a type (press-post).
I want to be able to pass in a URL parameter of a post, which will offset the query to start at the index of the post slug.
For example, www.mysite.com/press should start at offset 0, where as www.mysite.com/press?post=third-post-slug should query at offset 2.
How can I find the offset of a post in a query by its slug?
Here is my code so far:
if ($_GET['post']):
$post_offset = // Here I need to get the offset of that post in the query;
else:
$post_offset = 0;
endif;
$args = array(
'post_type' => 'press',
'post_status' => 'publish',
'posts_per_page' => 15,
'offset' => $post_offset,
'paged' => $page_no,
'orderby' => 'date',
'order' => 'DESC',
);
$press_posts = get_posts($args);
Related
I want to show a random selection of 4 new products in WooCommerce.
For that I'm using a first loop to get the 20 newest products.
Like this:
$args= array(
'post_type' => 'product',
'posts_per_page' => 20,
'orderby' => 'date',
);
Now I've a second loop to reduce the products to 4 in a random order:
$args_new = array(
'posts_per_page' => 4,
'orderby' => 'rand',
);
In the end I merge the two loops:
$final_args = array_merge( $args, $args_new );
But that doesn't work. Is there any other way to achieve it?
General knowledge
The post_type argument accept String or Array.
Argument
Description
post_type
(String/Array) – use post types. Retrieves posts by post types, default value is post. If tax_query is set for a query, the default value becomes any.
Source # https://developer.wordpress.org/reference/classes/wp_query/#post-type-parameters
Merging queries
In crude terms we want to combine 2 posts queries, retrieve each posts ID, push them to a new array and open a new query.
Keep in mind that if you want your 4 posts to be random (as you stated in the comments) they might be some duplicates of the last 20 from the first query. Don't forget to offset the second query.
<?php
// First query
$args_1 = get_posts( array(
'post_type' => 'dogs',
'post_status' => 'publish',
'post_count' => 20,
) );
// Second query
$args_2 = get_posts( array(
'post_type' => 'dogs',
'post_status' => 'publish',
'post_count' => 4,
'offset' => 20,
'orderby' => RAND,
) );
// Merge queries
$posts = array_merge( $args_1, $args_2 );
// Push posts IDs to new array
$identifiers = array();
foreach ( $posts as $post ) {
array_push( $identifiers, $post->ID );
};
// Third query
$query = new WP_Query( array(
'post_status' => 'publish',
'post_count' => 24,
'post_in' => array_unique( $identifiers ),
) );
var_dump( $query );
thank you for taking the time to review this.
I am calling a new WP_Query with the following arguments:
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'post__not_in' => array($featured_post_ID),
'category_name' => 'blog',
'offset' => $offset,
'posts_per_page' => 3,
'orderby' => 'post-date',
'order' => 'DESC',
);
I know that past issues have been caused by passing a numeric post ID as a variable instead of string; to get around that I am using the following variable definition for $featured_post_ID:
$featured_post_ID = '"'.$post_ID.'"';
When I run this query, it outputs the post I am trying to exclude.
Here is a Transcript of output of the WP_Query to save space. Looks like post__not_in is recognized in the query, but has no effect on the output.
Can anyone advise why this might be happening, or how to fix it?
I want to generate few random links for my wordpress but i don't want to use order by rand as it will cause heavy load on mysql. below links works fine but when i use orderby rand
$wprpi_arg = array(
'numberposts' => $wprpi_value['post'],
'post__not_in' => array(get_the_ID()),
// 'where ID <' => get_the_ID(),
'orderby' => 'rand',
'post_status' => 'publish',
);
i want to use this instead
$wprpi_arg = array(
'numberposts' => $wprpi_value['post'],
'post__not_in' => array(get_the_ID()),
'where ID <' => get_the_ID(),
'orderby' => 'ID',
'post_status' => 'publish',
);
But this is not working. WordPress is listing only 5 last links of table as it is orderby id its showing 5 links in desc order.
But i want to generate 5 links lower than my current id.
For example: if users is on page /10000 the generated links should be 9999,9998,9997,9996,9995 and if user is on /100 page generated links will be 99,98,97,96,95 but wordpress ignores where clause and generates same link always orderby desc limit 5 without considering where clause
You can use custom where condition in your wordpress query as below.
Note: Use get_the_ID() instead of 215 in your case
//Add Filter
add_filter( 'posts_where' , 'posts_where_for_ID' );
function posts_where_for_ID( $where ) {
$where .= ' AND ID < '. 215;
return $where;
}
//Create args
$args = array(
'numberposts' => 5, //$wprpi_value['post']
'post__not_in' => array(215),
'orderby' => 'ID',
'order' => 'DESC',
'post_status' => 'publish',
'suppress_filters' => FALSE
);
//Get the posts
$desc_posts = get_posts($args);
//Store post ids
$p_ids = array();
foreach($desc_posts as $posts_) {
$p_ids[] = $posts_->ID;
}
//Your ids are here
var_dump($p_ids);
This will applied to all get_posts() query in which suppress_filters passed in args.
If you have custom post_type then ckeck for post type or if you have single page then add in that page only.
I'm trying to do a query with the arguments below. Somehow, WP just doesn't return any posts. I can't figure out what I'm doing wrong! Any help?
Some more info: I have a custom post type that contain featured images. I want them to be displayed in a header slider. Through Advanced Custom Fields plugin I've created a custom field in the posts: 'assigned_page'. It's an array with page ID's on which that specific slide should be displayed. '$current_page' is the ID of the current page that's to be displayed. So, $args should filter the custom post type, and the posts that have the current page ID in their 'assigned_page' array.
// Get the current page ID
global $post;
$current_page = $post->ID;
$string_page = (string)$current_page;
$current_parent_page = $mv_is_subpage->ID;
// Post selection
$args = array (
'post_type' => $post_type,
'posts_per_page' => $posts_per_page,
'orderby' => $orderby,
'order' => $order,
'no_found_rows' => 1,
'meta_query' => array(
array(
'meta_key' => 'assigned_page',
'meta_value' => $string_page,
)
),
);
Then:
$query = new WP_Query( $args );
And then the loop:
while ($query->have_posts()) : $query->the_post();
I guess it should be like this (according to docs at http://codex.wordpress.org/Class_Reference/WP_Query) :
array(
'key' => 'assigned_page',
'value' => $string_page,
)
In the Get Terms(); WordPress function, you can get terms using the "name__like" parameter to only get terms that start with whatever characters you enter.
I need to use a new WP_Query, and I can't find a similar parameter:
$my_qry = new WP_Query( array( 'post_type' => 'post', 'posts_per_page' => -1, 'orderby' => 'title', 'order' => 'DESC', 'post_status' => 'publish' ) );
This gives me an array of all the posts published. How can I filter the found posts to only show posts that start with certain words/characters (by post title)?