Sort by price wpdb
I have a page on my wordpress to search for tires everything works fine but I would have liked to know how to add an extra column to sort them also by price.
They can already be sorted by size and type... But I can't find a solution for the price.
Thanks in advance !
Here is the code :
?php
$resultLargura = $wpdb-get_results (SELECT wp_t.name FROM $wpdb-term_taxonomy AS wp_tt
INNER JOIN $wpdb-terms AS wp_t ON wp_t.term_id = wp_tt.term_id
WHERE wp_tt.taxonomy = 'pa_largeur'ORDER BY wp_t.name ASC);
$resultAltura = $wpdb-get_results (SELECT wp_t.name FROM $wpdb-term_taxonomy AS wp_tt
INNER JOIN $wpdb-terms AS wp_t ON wp_t.term_id = wp_tt.term_id
WHERE wp_tt.taxonomy = 'pa_hauteur' ORDER BY wp_t.name ASC);
$resultDiametro = $wpdb-get_results (SELECT wp_t.name FROM $wpdb-term_taxonomy AS wp_tt
INNER JOIN $wpdb-terms AS wp_t ON wp_t.term_id = wp_tt.term_id
WHERE wp_tt.taxonomy = 'pa_diametre' ORDER BY wp_t.name ASC);
$resultTipoPneu = $wpdb-get_results (SELECT wp_t.name FROM $wpdb-term_taxonomy AS wp_tt
INNER JOIN $wpdb-terms AS wp_t ON wp_t.term_id = wp_tt.term_id
WHERE wp_tt.taxonomy = 'pa_type-de-pneu' ORDER BY wp_t.name);
$resultDePneu = $wpdb-get_results (SELECT wp_t.name FROM $wpdb-term_taxonomy AS wp_tt
INNER JOIN $wpdb-terms AS wp_t ON wp_t.term_id = wp_tt.term_id
WHERE wp_tt.taxonomy = 'pa_marque-pneus' ORDER BY wp_t.name);
$resultDePneus = $wpdb-get_results (SELECT wp_t.name FROM $wpdb-term_taxonomy AS wp_tt
INNER JOIN $wpdb-terms AS wp_t ON wp_t.term_id = wp_tt.term_id
WHERE wp_tt.taxonomy = 'pa_categories-de-pneus' ORDER BY wp_t.name);
?
form class=form-tires action=/resultados-da-pesquisa method =GET
h3 style=color: #222 !importantRecherche par dimension/h3
label class=input floatleft
select class=fullwidth floatleft ?php /*required*/? name=largura
option value=Largeur/option
?php
foreach( $resultLargura as $value ) { ?
option value=?php echo $value-name; ??php echo $value-name; ?/option
?php
}
?
/select
/label
label class=input floatleft
select class=fullwidth floatleft ?php /*required*/? name=altura
option value=Hauteur/option
?php
foreach( $resultAltura as $value ) { ?
option value=?php echo $value-name; ??php echo $value-name; ?/option
?php
}
?
/select
/label
label class=input floatleft
select class=fullwidth floatleft ?php /*required*/? name=diametro
option value=Diamètre/option
?php
foreach( $resultDiametro as $value ) { ?
option value=?php echo $value-name; ??php echo $value-name; ?/option
?php
}
?
/select
/label
div class=pneu-detailimg src=/wp-content/uploads/pneusahc.png/div
label class=fullwidth floatleft
select class=fullwidth floatleft required name=tipoPneu
?php
foreach( $resultTipoPneu as $value ) { ?
option value=?php echo $value-name; ??php echo $value-name; ?/option
?php
}
?
/select
/label
label class=fullwidth floatleft
select class=fullwidth floatleft required name=DePneu
?php
foreach( $resultDePneu as $value ) { ?
option value=?php echo $value-name; ??php echo $value-name; ?/option
?php
}
?
/select
/label
label class=fullwidth floatleft
option style=color: black; font-weight: bold; Plus d'indications :/option
select type=checkbox class=fullwidth floatleft name=DePneus
option value=Aucun/option
?php
foreach( $resultDePneus as $value ) { ?
option value=?php echo $value-name; ??php echo $value-name; ?/option
?php
}
?
/select
/label
label class=fullwidth floatleft
option style=color: black; font-weight: bold; Choix du prix :/option
select type=checkbox class=fullwidth floatleft name=posts_this_price
option value=Aucun/option
?php
foreach( $posts_this_price as $value ) { ?
option value=?php echo $value-name; ??php echo $value-name; ?/option
?php
}
?
/select
/label
input type=submit value=RECHERCHE name=PESQUISAR class=fullwidth floatleft
/form
And the other page :
?php
$optionLargura = isset($_GET['largura']) ? $_GET['largura'] : false;
$optionAltura = isset($_GET['altura']) ? $_GET['altura'] : false;
$optionDiametro = isset($_GET['diametro']) ? $_GET['diametro'] : false;
$optionTipoPneu = isset($_GET['tipoPneu']) ? $_GET['tipoPneu'] : false;
$marqueDePneu = isset($_GET['DePneu']) ? $_GET['DePneu'] : false;
$catDePneus = isset($_GET['DePneus']) ? $_GET['DePneus'] : false;
$coin=CHF;
$tax_query = array('relation' = 'AND');
if ($optionLargura!='')
{
$tax_query[] = array(
'taxonomy' = 'pa_largeur',
'field' = 'slug',
'terms' = array($optionLargura),
'operator' = 'IN',
);
}
if ($optionAltura!='')
{
$tax_query[] = array(
'taxonomy' = 'pa_hauteur',
'field' = 'slug',
'terms' = array($optionAltura),
'operator' = 'IN',
);
}
if ($optionDiametro!='')
{
$tax_query[] = array(
'taxonomy' = 'pa_diametre',
'field' = 'slug',
'terms' = array($optionDiametro),
'operator' = 'IN',
);
}
if ($optionTipoPneu!='')
{
$tax_query[] = array(
'taxonomy' = 'pa_type-de-pneu',
'field' = 'slug',
'terms' = array($optionTipoPneu),
'operator' = 'IN',
);
}
if ($marqueDePneu!='')
{
$tax_query[] = array(
'taxonomy' = 'pa_marque-pneus',
'field' = 'slug',
'terms' = array($marqueDePneu),
'operator' = 'IN',
);
}
if ($catDePneus!='')
{
$tax_query[] = array(
'taxonomy' = 'pa_categories-de-pneus',
'field' = 'slug',
'terms' = array($catDePneus),
'operator' = 'IN',
);
}
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) { // 'page' is used instead of 'paged' on Static Front Page
$paged = get_query_var('page');
} else {
$paged = 1;
}
$args = array(
'post_type' = array('product', 'product_variation'),
'post_status' = 'publish',
'posts_per_page' = get_option('posts_per_page'),
'paged' = $paged,
'tax_query' = $tax_query,
'orderby' = 'price',
'order' = 'asc',
);
$prods=new WP_Query($args);
if( $prods-have_posts() ){
$content = 'form class=cart action=https://ahcor-autoshop.ch/panier method=post enctype=multipart/form-data wtx-context=D6ECEB0A-195E-4229-9B5D-1A13E3D7E5AF';
while($prods-have_posts()) :
$product=$prods-the_post();
$content .= 'div class=search-col id=search-product-'.get_the_ID().'';
$attachment_url='div class=w10 floatleftimg alt=' . get_the_title() . ' src=https://ahcor-autoshop.ch/wp-content/uploads/pneusahc.png width=80/div';
$attachment_id=get_post_thumbnail_id(get_the_ID());
if($attachment_id){
$post_attachment_url=wp_get_attachment_url($attachment_id);
if($post_attachment_url)$attachment_url='div class=w15 floatleftimg alt=' . get_the_title() . ' src='.$post_attachment_url.' width=80/div';
}
$content.=$attachment_url;
//$content .= 'div class=w60 floatlefta href=https://ahcor-autoshop.ch/produit/'.$row-post_name.'/' . $row-post_title . '/a';
$content .= 'div class=w60 floatlefta href='.get_the_permalink().'' . get_the_title() . '/a';
$content .= 'div class=fullwidth floatleftsmall class=inline floatleftTags/small/div';
$price=get_post_meta(get_the_ID(),'_regular_price',true);
if(date(Y-m-d) != 2020-12-30)
{
$calcAscTotal= $price ;
$content .= 'h3 style=color:#d52b1e;del style=opacity: .5; display: inline-block; margin: 15px;color: #545454;/del CHF '.round($calcAscTotal, 0, PHP_ROUND_HALF_UP).' .- '.'/h3';
}
else
{
$content .= 'h3smallSeulement/small '. $price.' CHF.- '.'/h3';
}
$content .= '/div';
$content .= 'button type=submit name=add-to-cart value='. get_the_ID() .' class=single_add_to_cart_button button alt w20 floatrightPanier/button';
//$content .= '/div';
$content.='/div';
endwhile;
$content.= 'a onclick=window.history.go(-1); return false; href=# class=text-red center-el bold style=font-size:2em; padding: 20px 0Retour/a';
$content.='/form';
}
else{
$content.=h2 class='text-center'.__('Aucun résultat trouvé','ahcor-autoshop')../h2;
$content.= 'a onclick=window.history.go(-1); return false; href=# class=text-red center-el bold style=font-size:2em; padding: 20px 0Retour/a';
}
if($content != )echo $content;
if ($prods-max_num_pages 1) : // custom pagination
$orig_query = $wp_query; // fix for pagination to work
$wp_query = $prods;
?
nav class=prev-next-posts style=display: flex; justify-content: space-between; padding: 20px;
div class=next-posts-link class=text-red center-el bold style=font-size:2em;
?php echo get_previous_posts_link( 'Précédent' ); ?
/div
div class=prev-posts-link class=text-red center-el bold style=font-size:2em; text-align: right;
?php echo get_next_posts_link( 'Suivant', $prods-max_num_pages ); ?
/div
/nav
?php
$wp_query = $orig_query; // fix for pagination to work
?
?php endif; ?
form class=woocommerce-ordering method=get value=pneus
select name=orderby class=orderby aria-label=Commande
option value=menu_order selected=selectedTri par défaut/option
option value=popularityTri par popularité/option
option value=ratingTri par notes moyennes/option
option value=dateTri du plus récent au plus ancien/option
option value=priceTri par tarif croissant/option
option value=price-descTri par tarif décroissant/option
/select
input type=hidden name=paged value=1
/form