OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Wordpress loop: custom field value 'yes' > order by taxonomy 'a' and custom field value 'no' > order by taxonomy 'b'

  • Thread starter Thread starter wlp
  • Start date Start date
W

wlp

Guest
Tried searching and testing for two days now, but don't seem to get this loop working in the way that I want it. Any help would be greatly appreciated.

  1. I want to loop through a custom post type (I know how to do this)
  2. In this loop, I first would like to list a few posts (number can vary) with a custom field value ('yes'), and then sort these posts based on a taxonomy value (length).
  3. Then I would like to list the rest of the posts (these have a custom field value 'no') and sort these posts based on a taxonomy value (year).

These pages use pagination. I tried two different loops but since the first part is an undefined number of posts, this does not work for me. And on top of that it just feels 'cleaner' in one loop if possible.

So far this is what I have:

Code:
<?php $args = array(
   'post_type' => 'custom-post-type-name',
   'posts_per_page' => 24,
   'paged' => $paged,
   'meta_query' => array(
      'first-posts' => array(
         'meta_key' => 'highlighted-post',
         'meta_value' => 'yes',
       ),
      'other-posts' => array(
         'meta_key' => 'highlighted-post',
         'meta_value' => 'no'
      ), 
   ),
   'orderby' => array(
      'first-posts' => 'ASC',
      'other-posts' => 'DESC'
   ),
); ?>
<p>Tried searching and testing for two days now, but don't seem to get this loop working in the way that I want it. Any help would be greatly appreciated.</p>
<ol>
<li>I want to loop through a custom post type (I know how to do this)</li>
<li>In this loop, I first would like to list a few posts (number can vary) with a custom field value ('yes'), and then sort these posts based on a taxonomy value (length).</li>
<li>Then I would like to list the rest of the posts (these have a custom field value 'no') and sort these posts based on a taxonomy value (year).</li>
</ol>
<p>These pages use pagination. I tried two different loops but since the first part is an undefined number of posts, this does not work for me. And on top of that it just feels 'cleaner' in one loop if possible.</p>
<p>So far this is what I have:</p>
<pre><code><?php $args = array(
'post_type' => 'custom-post-type-name',
'posts_per_page' => 24,
'paged' => $paged,
'meta_query' => array(
'first-posts' => array(
'meta_key' => 'highlighted-post',
'meta_value' => 'yes',
),
'other-posts' => array(
'meta_key' => 'highlighted-post',
'meta_value' => 'no'
),
),
'orderby' => array(
'first-posts' => 'ASC',
'other-posts' => 'DESC'
),
); ?>
</code></pre>
Continue reading...
 
Top