Support » Fixing WordPress » Query within a query in a loop

  • Hi guys,

    I want to return posts in a loop which I have already done using the code below:

    $args = array(
    	'post_type' => 'company_listing',
    	'cat' => get_query_var('cat'),
    	//'meta_key' => 'featured',
    	//'order_by' => 'yes'
    $loop = new WP_Query( $args );

    Now I want to prioritise the posts depending on the meta key featured. If it is checked on the post, the post must appear first on the website = you know, that VIP treatment is what I am talking about here.

    How will I write a WP_Query?

Viewing 1 replies (of 1 total)
  • Here is some sample code that is nearly what you want:

    You must use filters to modify the query so that it creates a sort key that can be used in addition to other keys that you need.

    The sample code selects only posts that have been created within the last 24 hours. To remove that, you need to delete this line:

    $mam_global_where = " AND $wpdb->posts.post_date > '$now' ";

    Then change the orderby to put sort_key first, followed by any other fields that you need:

    $mam_global_orderby = "sort_key, $wpdb->posts.post_date DESC";
Viewing 1 replies (of 1 total)
  • The topic ‘Query within a query in a loop’ is closed to new replies.