I am trying to list records from db of a CPT as under:
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$taxanomiess = array('featured','mid-range','luxury','budget');
foreach($taxanomiess as $taxanomyy){
$args = array(
'post_type' => 'hotels',
'posts_per_page' => 20,
'post_status' => 'publish',
'orderby' => array('menu_order' => 'ASC', 'date' => 'DESC'),
'order' => 'DESC',
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'style',
'field' => 'slug',
'terms' => $taxanomyy,
'operator' => 'IN'
)
)
);
$loop = new WP_Query( $args );
?>
<ul class="hotel-list">
<?php
// Start the loop.
while ( $loop->have_posts() ) : $loop->the_post();
get_template_part( 'templates/hotel', 'page' );
endwhile; // End the loop.
PROBLEM:
I want to list all FEATURED hotels at the TOP of page and then MID RANGE and so on. But this query listing 20 records mixed (‘featured’,’mid-range’,’luxury’,’budget’) but in order as asked on every page. I am expecting to show all FEATURED records in few first pages then MID RANGE On next pages and so on.
2
Answers
Thank you Caleb for your detailed response and time.
I tried to solve the issue by writing custom query & joins, here is the code successfully running on my website.
endwhile; // End the loop. ?>
The simplest way of accomplishing this would be to set a meta key when a term is assigned to a hotel (using the
set_object_terms
action), with the ordering number in it:This will allow ordering the query by meta value:
You could accomplish the same functionality by setting the
menu_order
property to the correct number on term save, but that exposes a change in order too easily IMO.Note that this approach means that adding new terms in the future may require updating meta values. For example, if "Premium" is added as the second spot, then existing values greater than
1
will need to be changed to free the use of2
.Something like this (untested):