Forums

Order current category posts by custom value and exclude a tag (1 post)

  1. sunroots.net
    Member
    Posted 3 years ago #

    Hello,
    I am trying to modify the archive page creating a custom query which displays posts from the current category, ordering them by a custom field "postOrder" and excluding a tag "child".

    Anyone can help? I am not familiar with MySql and this is the first time I create a custom query.
    This is my query:

    $total = "
    		SELECT wposts.*
    		FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
    		WHERE wposts.ID = wpostmeta.post_id
    		AND wpostmeta.meta_key = 'postOrder'
    		AND wposts.post_type = 'post'
    		ORDER BY ABS(wpostmeta.meta_value) ASC
    		";
    
    		$totalposts = $wpdb->get_results($total, OBJECT);
    		$ppp = intval(get_query_var('posts_per_page'));
    		$wp_query->found_posts = count($totalposts);
    		$wp_query->max_num_pages = ceil($wp_query->found_posts / $ppp);
    		$on_page = intval(get_query_var('paged'));
    		if($on_page == 0){ $on_page = 1; }
    		$offset = ($on_page-1) * $ppp;
    
    	$wp_query->request = "
    		SELECT wposts.*
    		FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
    		WHERE wposts.ID = wpostmeta.post_id
    		AND wpostmeta.meta_key = 'postOrder'
    		AND wposts.post_type = 'post'
    		ORDER BY ABS(wpostmeta.meta_value) ASC
    		LIMIT $ppp
    		OFFSET $offset
    		";

Topic Closed

This topic has been closed to new replies.

About this Topic