我在wordpress上有一个页面可以搜索轮胎,一切都很好,但我想知道如何添加一个额外的列来按价格对它们进行排序。
它们已经可以按大小和类型排序了。。。但我找不到价格的解决方案。
提前感谢!
代码如下:
<?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 !important">Recherche 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-detail"><img 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 floatleft"><img 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 floatleft"><img alt="\' . get_the_title() . \'" src="\'.$post_attachment_url.\'" width="80"></div>\';
}
$content.=$attachment_url;
//$content .= \'<div class="w60 floatleft"><a href="https://ahcor-autoshop.ch/produit/\'.$row->post_name.\'/">\' . $row->post_title . \'</a>\';
$content .= \'<div class="w60 floatleft"><a href="\'.get_the_permalink().\'">\' . get_the_title() . \'</a>\';
$content .= \'<div class="fullwidth floatleft"><small class="inline floatleft">Tags</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 .= \'<h3><small>Seulement</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 floatright">Panier</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 0">Retour</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 0">Retour</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="selected">Tri par défaut</option>
<option value="popularity">Tri par popularité</option>
<option value="rating">Tri par notes moyennes</option>
<option value="date">Tri du plus récent au plus ancien</option>
<option value="price">Tri par tarif croissant</option>
<option value="price-desc">Tri par tarif décroissant</option>
</select>
<input type="hidden" name="paged" value="1">
</form>
SO网友:Valerii Vasyliev
代替
$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\',
);
新建代码
$orderby = $_GET[\'orderby\'] ?? \'\';
$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
);
if ($orderby == \'price\') {
$args[\'orderby\'] = \'meta_value_num\';
$args[\'meta_key\'] = \'_price\';
$args[\'order\'] = \'asc\';
} elseif ($orderby == \'price-desc\'){
$args[\'orderby\'] = \'meta_value_num\';
$args[\'meta_key\'] = \'_price\';
$args[\'order\'] = \'desc\';
} elseif ($orderby == \'menu_order\') {
// sort menu order
} elseif ($orderby == \'popularity\') {
// sort popularity
} elseif ($orderby == \'rating\') {
// sort rating
} elseif ($orderby == \'date\') {
// sort date
}