• Resolved Shabir Virk

    (@sakuhetu)


    Hello Friends,

    I have a site where i m posting some of the post at Features with a meta key name “Listing Type” and meta value “Featured”

    Now the problem is when i use

    query_posts($query_string . '&meta_key=listing_type&orderby=Featured');

    only the Features post showing.

    All i just want to show is the featured posts and then the rest of the post by the date order with the pagination too..

    Please help me out

Viewing 10 replies - 1 through 10 (of 10 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    In what template file do you need this? The front page?

    Thread Starter Shabir Virk

    (@sakuhetu)

    Right now i want this in my category.php template

    But it would be better if it will be universal like all of the taxonomy and search results.

    Actually i am having multiple categories for different things

    like auto, property, jobs

    you can see the site here https://www.ekarnal.com/

    It is a city portal.

    Thanks

    Moderator keesiemeijer

    (@keesiemeijer)

    Try it without query_posts and with this in your theme’s functions.php file:

    function my_post_queries( $query ) {
    	// not an admin page and is the main query
    	if ( !is_admin() && $query->is_main_query() ) {
    		// category and search pages
    		if ( is_category() || is_search() ) {
    			add_filter( 'posts_orderby', 'add_featured_orderby' );
    		}
    	}
    }
    add_action( 'pre_get_posts', 'my_post_queries' );
    
    function add_featured_orderby( $orderby ) {
    	global $wpdb;
    
    	// get meta listing_type -> featured post ids
    	$args = array(
    		'meta_key' => 'listing_type',
    		'meta_value' => 'Featured',
    		'fields' => 'ids',
    		'order' => 'DESC',
    		'orderby' => 'date',
    		'posts_per_page' => -1,
    	);
    
    	if(is_category())
    		$args['cat'] = get_query_var('cat');
    
    	$posts= get_posts( $args );
    
    	if ( $posts ) {
    
    		// add custom ordering
    		$sql = ' CASE';
    		$i = count( $posts );
    		foreach ( $posts as $post ) {
    			$sql .= " WHEN $wpdb->posts.ID = $post THEN $i";
    			$i--;
    		}
    		$sql .= ' ELSE 0 END DESC, ';
    		echo $sql;
    
    		$orderby = $sql . $orderby;
    	}
    
    	return $orderby;
    }

    This will order the posts with a custom field listing_type with value “Featured” at the top on category and search archive pages.

    btw:
    consider creating a child theme instead of editing your theme directly – if you upgrade the theme all your modifications will be lost.

    Thread Starter Shabir Virk

    (@sakuhetu)

    Thanks keesiemeijer,

    It is now working but showing a text line before the header

    “CASE WHEN ke_posts.ID = 245 THEN 5 WHEN ke_posts.ID = 239 THEN 4 WHEN ke_posts.ID = 183 THEN 3 WHEN ke_posts.ID = 181 THEN 2 WHEN ke_posts.ID = 196 THEN 1 ELSE 0 END DESC, “

    https://www.ekarnal.com/cats/auto

    Could you please tell me how to fix

    Moderator keesiemeijer

    (@keesiemeijer)

    Ah, I left some testing code. Remove this:

    echo $sql;

    Thread Starter Shabir Virk

    (@sakuhetu)

    Oh thanks ??

    Everything is perfect.

    Also is there any other option to show a Platinum Feature in top of all?

    Means First Platinum Entries > Featured Entries > Simple Post

    Also there is some more modification i needed (if you can help).

    Right now all the posts are sorted by Date DSC, but i just want only the featured and Platinum listing will be ordered by Random and rest of the simple entries by date only.

    All people are paying me the same amount and the one who paid me few days back go down in the line.

    This is not a good thing, by random order anyone can show anywhere.

    Hope you are understanding my Concept

    Thanks ‘n’ Regards,
    Shabir Virk

    Moderator keesiemeijer

    (@keesiemeijer)

    If ‘platinum’ is also a value of the ‘listing_type’ custom field you can use this:

    function my_post_queries( $query ) {
    	// not an admin page and is the main query
    	if ( !is_admin() && $query->is_main_query() ) {
    		// category and search pages
    		if ( is_category() || is_tag() || is_tax() || is_search() ) {
    			add_filter( 'posts_orderby', 'add_featured_orderby' );
    		}
    	}
    }
    add_action( 'pre_get_posts', 'my_post_queries' );
    
    function add_featured_orderby( $orderby ) {
    	global $wpdb;
    
    	$args = array(
    		'meta_key' => 'listing_type',
    		'fields' => 'ids',
    		'orderby' => 'rand',
    		'posts_per_page' => -1,
    	);
    
    	if ( is_category() || is_tag() || is_tax() ) {
    		$obj = get_queried_object();
    		$tax_query = array(
    			array(
    				'taxonomy' => $obj->taxonomy,
    				'field' => 'id',
    				'terms' => $obj->term_id,
    			)
    		);
    		$args['tax_query'] = $tax_query;
    	}
    
    	$platinum_args = $args + array( 'meta_value' => 'platinum' );
    	$featured_args = $args + array( 'meta_value' => 'featured' );
    
    	$platinum = (array) get_posts( $platinum_args );
    	$featured = (array) get_posts( $featured_args );
    
    	$posts = array_unique( array_merge( $platinum, $featured ) );
    
    	if ( $posts ) {
    
    		// add custom ordering
    		$sql = ' CASE';
    		$i = count( $posts );
    		foreach ( $posts as $post ) {
    			$sql .= " WHEN $wpdb->posts.ID = $post THEN $i";
    			$i--;
    		}
    		$sql .= ' ELSE 0 END DESC, ';
    
    		$orderby = $sql . $orderby;
    	}
    
    	return $orderby;
    }

    This will order the custom field posts randomly and the rest by post date.

    This code will now also use this order on custom taxonomy and tag pages.

    Just remove the taxonomies you don’t want in:

    if ( is_category() || is_tag() || is_tax() || is_search() ) {
    			add_filter( 'posts_orderby', 'add_featured_orderby' );
    		}

    Thread Starter Shabir Virk

    (@sakuhetu)

    Thanksss ??

    Really you are awesome man saved my hours of searching.

    Moderator keesiemeijer

    (@keesiemeijer)

    You’re welcome ??

    Also wanted to thank you, this type of solution (putting Featured posts at top using a single query, without using hacktacular combinations of different wp_query results) is really hard to track down. Much appreciated, worked like a charm.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Order By Custom Field – Strange Problem’ is closed to new replies.