I didn’t understand your question…
Do you want only results for 161, 165, 131 and 202 ?
If so, you can use post__in, see: https://codex.wordpress.org/Class_Reference/WP_Query#Post_.26_Page_Parameters
Correct – results for those IDs in that particular order.
post__in should be ideal.
OK, so my code looks like
$query_args = array('posts_per_page' => $products, 'no_found_rows' => 1, 'post_status' => 'publish', 'post_type' => 'product', 'post__in' => array(161,165,131,202) );
$query_args['meta_query'] = $woocommerce->query->get_meta_query();
$query_args['meta_query'][] = array(
'key' => '_featured',
'value' => 'yes'
);
$r = new WP_Query($query_args);
but it displays them not in the order from that array but by looks like when a product was added to the back end.
Hey JackTheKnife,
Shooting in the dark here: Have you tried the orderby and order parameters? More information at: https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters
OK, added orderby argument
$query_args = array('posts_per_page' => $products, 'no_found_rows' => 1, 'post_status' => 'publish', 'post_type' => 'product', 'post__in' => array(161,165,131,202), 'orderby' => 'post__in' );
but still no luck with desired order :\
and created WP Query looks like
SELECT wp_posts.ID
FROM wp_posts
INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id )
INNER JOIN wp_postmeta AS mt1 ON ( wp_posts.ID = mt1.post_id )
WHERE 1=1
AND wp_posts.ID IN (161,165,131,202)
AND wp_posts.post_type = 'product'
AND ((wp_posts.post_status = 'publish'))
AND ( ( wp_postmeta.meta_key = '_visibility'
AND CAST(wp_postmeta.meta_value AS CHAR) IN ('visible','catalog') )
AND (mt1.meta_key = '_featured' AND CAST(mt1.meta_value AS CHAR) = 'yes' ))
GROUP BY wp_posts.ID
ORDER BY wp_posts.menu_order, FIELD( wp_posts.ID, 161,165,131,202 )
LIMIT 0, 5
Where ORDER BY wp_posts.menu_order came from?
Got it fixed.
Added
'suppress_filters' => true
to query arguments and
remove_all_actions( 'pre_get_posts' );
before query execution.
Looks like “poisoning” from the theme itself.
Instead of remove_all_actions pre_get_posts, add:
'ignore_custom_sort' => true
to the query arguments, e.g.:
$args = array(
'post_type' => 'page',
'post__in' => array(1,2,3),
'orderby' => 'post__in',
'suppress_filters' => true,
'ignore_custom_sort' => true
);
// The Query
$query = new WP_Query( $args );
This ignores the custom sort by the Post Types Order plugin (https://wordpress.org/plugins/post-types-order/). See CPTO_pre_get_posts() function in post-types-order.php
-
This reply was modified 9 years, 7 months ago by
chriswingman.
-
This reply was modified 9 years, 7 months ago by
chriswingman.
-
This reply was modified 9 years, 7 months ago by
chriswingman.