Ich versuche, meine WordPress-Site-Suche zu verbessern,
Ich möchte, dass Benutzer auch nach WooCommerce-Produktattributen suchen können. Ich habe WooCommerce-Produkte erstellt und alle benutzerdefinierten Attribute (Produktattribute) über diesen Code hinzugefügt:
$attributes = array(
'_image_number' => array(
'name' => 'IMAGE ID',
'value' =>0001,
'is_visible' => '1',
'is_variation' => '0',
'is_taxonomy' => '0'
),
'_year' => array(
'name' => 'YEAR',
'value' => 1988),
'is_visible' => '1',
'is_variation' => '0',
'is_taxonomy' => '0'
),
);
update_post_meta( $new_post_id, '_product_attributes', $attributes );
In Ordnung, Mein Produkt wird erstellt und diese Attribute werden in Postmeta gespeichert als meta_key=_product_attributes
und meta_value=
serialisiertes Array meiner Attribute.
Mein Suchformular ist:
<form action="<?php echo home_url(); ?>/" method="get" class="wp-search-form">
<i class="oic-zoom"></i>
<input type="text" name="s" id="search" placeholder="<?php echo get_search_query() == '' ? __('Type and hit Enter', 'vp_textdomain') : get_search_query() ?>" />
</form>
<br/>
Im functions.php
Ich habe den Filter pre_get_posts verwendet, um die Abfrage zu ändern:
function ozy_custom_search( $query ) {
if(!is_admin()) {
if ( isset($query->is_search) && $query->is_search ) {
$query->set( 'post_type', array( 'product', 'post', 'page', 'ozy_portfolio' ) );
$meta_query=array(
'key' => '_product_attributes',
'value' => $query->query['s'],
'compare' => 'EXISTS',
);
$query->set( 'meta_query', $meta_query );
/*
$meta_query=array(
array(
'key' => '_product_attributes',
'value' => $query->query['s'],
'compare' => 'EXISTS',
),
);
*/
// echo "<pre>";print_r($query);die;
}
}
return $query;
};
add_filter( 'pre_get_posts', 'ozy_custom_search' );
Das Ergebnis, das ich immer noch bekomme, ist, dass es in Post-Titel und Post-Inhalt sucht und nicht in Produktattributen-Meta gesucht wird.
Irgendwelche Vorschläge?
Hallo, hast du dafür eine Lösung gefunden?
– Jeff
13. Oktober 2018 um 11:42 Uhr
Ist nicht der genauere Weg, aber mit Ihrem Code können Sie den Vergleichsparameter ändern. Versuchen Sie es mit “LIKE”
– Adrian Cobo
25. Juni 2019 um 16:09 Uhr