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.
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 );
I have this code, its working good, but its gets posts from all categories. I need to get posts only from category ID 2. I tried to add "AND term_taxonomy_id = 2" to my code, but this didint work. Can somebody help me with this, my code:
<?php //loops all posts
$my_query = $wpdb->get_results
("SELECT * FROM `{$table_prefix}posts`, {$table_prefix}postmeta
WHERE {$table_prefix}posts.post_status = 'publish'
AND {$table_prefix}posts.id = {$table_prefix}postmeta.post_id
AND {$table_prefix}postmeta.`meta_key` = 'wpcf-data-nuo' ORDER BY if({$table_prefix}postmeta.`meta_value` = '' or {$table_prefix}postmeta.`meta_value` is null,1,0), {$table_prefix}postmeta.`meta_value` ASC LIMIT 12");
foreach($my_query as $post) {
setup_postdata($post);
?>
Please follow below code, Hope! its working
You can define the meta key for orderby parameter using the old method (I tested on WP 3.1.1)...
query_posts(
array( 'post_type' => 'services',
'order' => 'ASC',
'meta_key' => 'some_key',
'orderby' => 'meta_value', //or 'meta_value_num'
'meta_query' => array(
array('key' => 'order_in_archive',
'value' => 'some_value'
)
)
)
);
OR
For me, I wanted to order by a numeric field and I had to use 'type' => 'NUMERIC' inside the meta query.
This issue in general is cleared up in WordPress 4.2 by using named queries. e.g.
$args = array(
'post_type' => 'services',
'orderby' => 'order_clause',
'meta_query' => array(
'order_clause' => array(
'key' => 'order_in_archive',
'value' => 'some_value',
'type' => 'NUMERIC' // unless the field is not a number
)));
Reference link for set the meta value in query: https://rudrastyh.com/wordpress/meta_query.html
You can do it by WP_QUERY instead writing custom Query.
The query to get the results and then your custom sort order by altering the WP_QUERY if default order parameters not work as expected.
$args = array(
'post_type' => 'post',
'posts_per_page' => 8,
'cat' => 2,
'meta_key' => 'wpcf-data-nuo',
'orderby' => 'meta_value_num',
'order' => 'ASC'
);
add_filter('posts_orderby', 'filter_query');
$q = new WP_Query($args);
remove_filter('posts_orderby', 'filter_query');
function filter_query( $orderby_statement ) {
global $table_prefix;
$orderby_statement .= " if({$table_prefix}postmeta.`meta_value` = '' ";
$orderby_statement .= " or {$table_prefix}postmeta.`meta_value` is null,1,0), {$table_prefix}postmeta.`meta_value`";
return $orderby_statement;
}
Note: Do not remove spaces from $orderby_statement .= because it require spaces while running query.
If it still doesn't work, then add var_dump($orderby_statement); before return $orderby_statement; then copy the SQL and add it in your question so it will help us to understand what is the issue.
I would like to get the first audio file in the media library filtered by a custom field, but it always returns an empty set of posts. I know that I have to pass 'post_status' => 'inherit' and 'post_type' => 'attachment', but it doesn't change anything.
<?php
// Arguments
$args = array(
'post_type' => 'attachment',
'post_status' => 'inherit',
'post_mime_type' => 'audio',
'meta_key' => 'my_meta_key',
'meta_value' => 'my_meta_value',
'posts_per_page' => 1 );
// Create the query
$audio_files = new WP_Query( $args );
// Output
var_dump( $audio_files ); // the number of found posts is always 0
?>
So I tried to minimise it by letting away all the meta-key-stuff and searched for any attachment (as mentioned here)
<?php
// Arguments
$args = array(
'post_type' => 'attachment',
'post_status' => 'inherit' ); // or "any", but without effect
// Create the query
$any_files = new WP_Query( $args );
// Output
var_dump( $any_files ); // same: the number of found posts is always 0
?>
So I tried it the old-fashioned way by a custom SQL-statement:
<?php
// Get row by custom SQL-statement
$wpdb->get_row( 'SELECT * FROM ' . $wpdb->prefix . 'posts p, ' . $wpdb->prefix . 'postmeta m WHERE p.post_mime_type LIKE "audio/%" AND p.ID = m.post_ID AND m.meta_key = "my_meta_key" AND m.meta_value = "my_meta_value" ORDER BY p.post_date DESC' );
?>
And tataaaa! I get my first audio file.
I know there are similar questions:
Broken? WP_Query and “attachment” as a post type
WP_Query not working as expected for attachments and custom meta_query
But neither of them helped me, even though they were treating quite the same topic.
What I am missing here? I would like to use WP_Query.
you should use offset,orderby and order when you are using posts_per_page it should definitely work. here is your revised code
if posts_per_page is -1 then it will list all post so in that case offset is ignored but in all other case you should use.
$args = array(
'post_type' => 'attachment',
'post_status' => 'inherit',
'offset' => $offset,
'post_mime_type' => 'audio',
'meta_key' => 'my_meta_key',
'orderby' =>'ID',
'order' => 'ASC',
'meta_value' => 'my_meta_value',
'posts_per_page' => 1 );
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)?