WordPress.org

Ready to get started?Download WordPress

Forums

Query within a query in a loop (2 posts)

  1. Archie22is
    Member
    Posted 1 year ago #

    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?

  2. vtxyzzy
    Member
    Posted 1 year ago #

    Here is some sample code that is nearly what you want: http://pastebin.com/E5ne7J0n

    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";

Topic Closed

This topic has been closed to new replies.

About this Topic