Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WordPress- Get post from Category by Priority

I want get 3 posts from event custom post type. But i want to do that by category priority. If "featured" category has 3 upcoming event then i want to show 3 posts from it, if "featured" category doesn't have 3 upcoming events then i want to get posts from other category+posts without any category too.

The end result will return 3 upcoming event. I'm a little confuse about how can i add that condition parameter on WP_Query.

Current code(that only return maximum 3 posts from featured category)-

$events_args = array(
      'post_type'     => 'ctc_event',                
      'meta_query' => array(
        array(
          'key' => '_ctc_event_end_date',
          'value' => date_i18n( 'Y-m-d' ),
          'compare' => '>=',
          'type' => 'DATE'
        ),
      ),
      'meta_key'      => '_ctc_event_start_date_start_time',
      'meta_type'     => 'DATETIME',
      'orderby'     => 'meta_value',
      'order'       => 'ASC',
      'posts_per_page' => 3,
      'tax_query' => array(
          array(
              'taxonomy' => 'ctc_event_category',
              'field'    => 'slug',
              'terms'    => 'featured',
          ),
      ),
    );

$events = new WP_Query( $events_args );

Thanks in advance.

like image 673
Arif Khan Avatar asked Mar 26 '26 17:03

Arif Khan


1 Answers

Don't create a category for "featured", create a custom field instead. And in your query remove the 'tax_query' to get all the posts and change the orderby 'orderby' => 'the_field_you_created'

like image 125
Ed Dogan Avatar answered Mar 29 '26 06:03

Ed Dogan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!