Reputation: 69
I want to display posts order by thumbnails i.e the post having thumbnails will display first and then display posts without thumbnail.
I found same situation on Stackoverflow here , But that doesn't work for me. Currently i am using directory+ theme in which i want to implement it.
I tried to make two loops, one loop will display post with thumbnail and second will display posts without thumbnail. But this doesnot work in case of pagination, as the post without thumbnail should on the last page
Here is the code
{var $itemQuery = array(
'post_type' => 'ait-item',
'posts_per_page' => $filterCountsSelected,
//'meta_key'=> '_thumbnail_id',
'tax_query' => array(
array(
'taxonomy' => 'ait-items',
'field' => 'term_id',
'terms' => $currentCategory->term_id
)
),
'paged' => $pagination,
)}
{customQuery as $query, $itemQuery}
{* Loop for categories with featured Image*}
{customLoop from $query as $post}
{if $post->image}
//Display post with thumbnail
{/if} //end of if
{/customLoop} //end of loop
{* Loop for categories without featured Image*}
{customLoop from $query as $post}
{if !$post->image}
//Display post without thumbnail
{/if} //end of if
{/customLoop} //end of loop
{includePart parts/pagination, location => pagination-below, max => $query->max_num_pages} //includes pagination
Edited Query 1:
{var $itemQuery = array(
'post_type' => 'ait-item',
'posts_per_page' => $filterCountsSelected,
'tax_query' => array(
array(
'taxonomy' => 'ait-items',
'field' => 'term_id',
'terms' => $currentCategory->term_id
)
),
'paged' => $pagination,
)}
{var $itemQuery['orderby'] = 'meta_value_num'}
{var $itemQuery['meta_query'] = array(
array(
'relation' => 'OR',
array(
'key' => '_thumbnail_id',
'compare' => 'NOT EXISTS'
),
array(
'key' => '_thumbnail_id',
'compare' => '!NOT EXISTS'
),
)
)}
Upvotes: 3
Views: 2824
Reputation: 5311
I would sort it via foreach assigning number variable if post has thumbnail then used array_multisort
try this,
function cmk_load_post_by_custom_order() {
// Query Argument
$args = array(
'post_type' => 'post',
'posts_per_page' => 6,
);
//Initization Wordpress Query
$query = new WP_Query( $args );
$posts = $query->get_posts();
$output = array();
$order = array();
//Ourput result as Custom Array
foreach( $posts as $post ) {
$has_thumb = has_post_thumbnail( $post->ID ) ? 1 : 0 ;
$output[] = array(
'title' => $post->post_title,
'slug' => $post->post_name,
'thumb' => $has_thumb,//get_the_post_thumbnail( $post->ID,'full'),
);
$order[] = $has_thumb;
}
array_multisort($order, SORT_DESC, $output);
echo '<pre>',print_r($output,1),'</pre>';
}
add_action('wp_head','cmk_load_post_by_custom_order');
EDIT 1
My answer above would not work if you are going to use wordpress pagination as pagination relies on query object,
I've done some test and I've come up with the idea of using the meta_value_num
on orderby
then set _thumbnail_id
as meta_key and add relation OR if either _thumbnail_id
is empty or not, as post without thumbnail will have empty _thumbnail_id
key.
try this query argument below
$args = array(
'post_type' => 'post',
'posts_per_page' => 99,
'order' => 'DESC', // Display all post with thumbnail ID first
'orderby' => 'meta_value_num', // order by meta key value
'meta_query' => array( //query post based on meta key
array(
'relation' => 'OR', // add condition if meta key is exists or not
array(
'key' => '_thumbnail_id',
'compare' => 'NOT EXISTS' // include post without _thumbnail_id key
),
array(
'key' => '_thumbnail_id',
'compare' => '!NOT EXISTS' // include post with _thumbnail_id key
)
)
)
);
I tested it locally and I got these following result, it query posts with and without thumbnail, I included the thumbnail ID on the result and you can see that the post is ordered base on thumbnail value
Array
(
[0] => Array
(
[title] => The Magnificent
[slug] => the-magnificent
[thumb] => http://dynamized.dev/dyna/wp-content/uploads/2015/09/happy-hour.png
[tuhmb_id] => 17
)
[1] => Array
(
[title] => The Awesome Company
[slug] => the-awesome-company
[thumb] => http://dynamized.dev/dyna/wp-content/uploads/2015/09/comedy.png
[tuhmb_id] => 16
)
[2] => Array
(
[title] => Selfieties
[slug] => selfieties
[thumb] => http://dynamized.dev/dyna/wp-content/uploads/2015/09/beverage-store.png
[tuhmb_id] => 13
)
[3] => Array
(
[title] => Drunkard
[slug] => drunkard
[thumb] => http://dynamized.dev/dyna/wp-content/uploads/2015/09/restaurant31.png
[tuhmb_id] => 9
)
[4] => Array
(
[title] => My First Company
[slug] => my-first-company
[thumb] => http://dynamized.dev/dyna/wp-content/uploads/2015/09/party.png
[tuhmb_id] => 7
)
[5] => Array
(
[title] => No Thumb Compnay
[slug] => no-thumb-compnay
[thumb] =>
[tuhmb_id] =>
)
)
EDIT 2
You can also try using array value on orderby
and use meta_value_num
with fallback to date
which display all posts with thumbnail id then sorted by date then all posts without thumbnail ID
change the argument parameter to this,
'orderby' => 'date, meta_value_num', // order by meta key value
Upvotes: 5