Support

Account

Home Forums Front-end Issues Display Featured Items First

Solving

Display Featured Items First

  • Hi,

    I have created a event post type and added custom fields start and end date and i have created custom archive page for it and I am displaying events orderby start date

    <?php
    
    // query events order
    $posts = get_posts(array(
    	'posts_per_page'	=> -1,
    	'post_type'			=> 'event',
    	'order'				=> 'ASC',
    	'orderby'			=> 'meta_value',
    	'meta_key'			=> 'start_date',
    	'meta_type'			=> 'DATETIME'
    ));
    
    if( $posts ): ?>
    
    	<h2>All Events</h2>
    	<ul id="events">
    		<?php foreach( $posts as $p ): ?>
    			<li>
    				<strong><?php echo $p->post_title; ?></strong>: <?php the_field('start_date', $p->ID); ?> -  <?php the_field('end_date', $p->ID); ?>
    			</li>	
    		<?php endforeach; ?>
    	</ul>
    
    <?php endif; ?>
    

    And I have created a Featured custom field True/False Now I want to show featured events first at the same time orderby start date

    Please help on this.

  • In order to do this you need to use meta_query instead of meta_key and meta_type.

    For more information about ordering by a custom field see the last code example in the order by section here https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters and this page https://make.wordpress.org/core/2014/08/29/a-more-powerful-order-by-in-wordpress-4-0/

  • Hi,

    I am confused between the keys and values.

    Would you give some example code to achieve this.

    Thanks.

  • the query arguments would look something like this

    
    $args = array(
      'posts_per_page'  => -1,
      'post_type'      => 'event',
      'meta_query' => array(
        'date_clause' => array(
          'key' => 'start_date',
          'compare' => 'EXISTS'
        ),
        'feature_clause' => array(
          'key' => 'featured', // or whatever your t/f field name is
          'compare' => 'EXISTS'
        )
      ),
      'orderby' => array(
        'feature_clause' => 'DESC',
        'date_clause' => 'ASC'
      )
    );
    
  • <?php
    			// query events order
    			$posts = get_posts(array(
    				'posts_per_page'	=> -1,
    				'post_type'			=> 'event',
    				'meta_query'		=> array(
    					'date_clause'		=> array(
    						'key'				=> 'start_date',
    						'compare'			=> 'EXISTS'
    					),
    					'feature_clause'	=> array(
    						'key'				=> 'featured',
    						'compare'			=> 'EXISTS'
    					)
    				),
    				'orderby'			=> array(
    					'feature_clause'	=> 'DESC',
    					'date_clause'		=> 'ASC'
    				)
    			));
    
    			if( $posts ): ?>
    				<div class="events-list">
    					<?php foreach( $posts as $p ): ?>
    						<div><h2><a href="<?php echo get_permalink( $p->ID ); ?>"><?php echo $p->post_title; ?></a></h2></div>
    						<div><?php the_field('start_date', $p->ID); ?></div>
    						<div><?php the_field('end_date', $p->ID); ?></div>
    					<?php endforeach; ?>
    				</div>
    			<?php endif; ?>

    I have write the code like this and it’s working (is it correct?)

    And Thank you very much.

  • Hi I have tried this for active events, featured is coming first ok but the date orderby is not working (after featured events latest posted events coming next)

    // find date time now
    				$date_now = date('Y-m-d H:i:s');
    
    				// query events
    				$posts = get_posts(array(
    					'posts_per_page'	=> -1,
    					'post_type'			=> 'event',
    					'meta_query' 		=> array(
    						'date_clause'	=> array(
    						'relation' 			=> 'AND',
    							array(
    								'key'			=> 'start_date',
    								'compare'		=> '<=',
    								'value'			=> $date_now,
    								'type'			=> 'DATETIME'
    							),
    							array(
    								'key'			=> 'end_date',
    								'compare'		=> '>=',
    								'value'			=> $date_now,
    								'type'			=> 'DATETIME'
    							)
    						),
    						'feature_clause'	=> array(
    						'key'				=> 'featured',
    						'compare'			=> 'EXISTS'
    						)
    					),
    					'orderby'			=> array(
    					'feature_clause'	=> 'DESC',
    					'date_clause'		=> 'ASC'
    					)
    				)); 

    Please help on this (I want to show featured events first next active events based on end date first)

    Please help on this

  • You cannot put a nested query inside of the “clause” date query. I’m not sure how you can do this, or even if it’s possible.

  • Is there any solution for this? Any help.

  • You need to have two different meta queries for the date. One like you have to get the current or active events, this query will not have a “clause”. The second query will be the “EXISTS” query I posted earlier.

    Also, you need to check the format of the data that ACF stores. If this is a “Date” field, ACF may not be storing in a standard “Date” format and type' => 'DATETIME' is probably the wrong type to use.

  • You should use php’s usort. The code below:

    1. creates a function that grabs the featured post’s ID (set in a custom field assigned to the posts page)
    2. checks posts in the query and compares two at a time, moving $post_a to the front if its ID matches the featured post ID.

    usort( $wp_query->posts, function ( $post_a, $post_b ) {
        
        $featured = get_field('featured_post', $page)[0];
    
        $a = $post_a->ID == $featured;
        $b = $post_b->ID == $featured;
    
        if ( !$a && !$b ) { // both false
            return 0;
        }
        return ( $a ) ? - 1 : 1;
    } );
    
Viewing 11 posts - 1 through 11 (of 11 total)

The topic ‘Display Featured Items First’ is closed to new replies.