mercredi 4 février 2015

Want to order by Meta Values (tweaking Post Order Widget)


I'm not much of a coder, but I can tweak PHP a bit. Basically I want to order by a custom meta value which integer called "price". The plugin orders by title, so thats what I'm changing to price, shown here. I saw some other posts about ordering by a meta value integer but the code logic was too different for my poor coder skills to reconcile with code in the plugin. Any help?



case 'title':
$orderby = "price";
$order = "ASC";

break;


Here's the entire function:



public function frontend_initialiser($vars){

//Requête modifiable uniquement pour la toute première requête SQL.
if(is_main_query() && $this->firstQuery){
$this->firstQuery = FALSE;

//On récupère la sélection de l'utilisateur
$this->selection = isset($_GET['pow']) ? $_GET['pow'] : "";

//Calcul du type (uniquement ceux gérés par l'extension) de taxonomie en cours.
if(is_category()){
$this->type = "category";
}elseif(is_tag()){
$this->type = "post_tag";
}

//Si le type est géré par l'extension, on récupère le slug courant.
if($this->type != '')$this->slug = $vars->query_vars[$this->type . "_name"];

//Le visiteur n'a pas demandé à changer le tri.
if($this->selection == ''){

//Par défaut, on garde le tri par défaut.
$this->selection = self::POW_DEFAUT;

//Maintenant, on va regarder si le tri par défaut ne serait pas différent de celui définit nativement
if($this->slug != ''){
//Recovering the conf different for different type of taxonomy
$terms = $this->select_term($this->type);

//If you look at the admin changed the default sort .
if(array_key_exists($this->type, $terms) && array_key_exists($this->slug, $terms[$this->type]))$this->selection = $terms[$this->type][$this->slug];
}

}

// We end up modifying the ORDER BY clause of the query.
switch ($this->selection) {

//Sélection par défaut : Tri du plus récent au plus ancien
case 'dated':
$orderby = "date";
$order = "DESC";

break;

//Tri du plus ancien au plus récent
case 'datea':
$orderby = "date";
$order = "ASC";

break;

//TRYING TO CHANGE CODE TO ORDER BY PRICE METAKEY VALUE
case 'title':
$orderby = "price";
$order = "ASC";

break;

//for simple cases , the value of the item of the combo is what will be used in the " order by " of the SQL query .
default:
$orderby = $this->selection;
$order = "DESC";

}

//On applique le filtre
$vars->set('orderby', $orderby);
$vars->set('order', $order);
}

return $vars;
}




Aucun commentaire:

Enregistrer un commentaire