Vous pouvez essayer de modifier la requête fiscale pour utiliser le relation
paramètre et ajoutez une deuxième clause qui correspond à n’importe quel message qui n’a pas le matched string
valeur dans le méta-tableau.
Voir Paramètres de taxonomie.
EDIT : Merci de l’avoir signalé, Tom. Vous avez raison, j’ai mis à jour pour refléter.
function group_matched_posts_at_top( $query ) {
// Check if this is the main query and not in the admin area
if( !$query->is_main_query() || is_admin() )
return;
// Define the tax query with two clauses (matched and not matched)
$taxquery = array(
'relation' => 'OR', // Set the relation to OR to include posts that match either clause
array(
'taxonomy' => 'mymeta',
'field' => 'slug',
'terms' => 'matchedstring',
'operator' => 'IN' // use the operator parameter to specify the comparison operator
),
array(
'taxonomy' => 'mymeta',
'field' => 'slug',
'terms' => 'matchedstring',
'operator' => 'NOT IN' // use the operator parameter to specify the comparison operator
)
);
// Set the tax query and meta key parameters for the query
$query->set( 'tax_query', $taxquery );
$query->set( 'meta_key', 'mymeta' );
// Set the orderby parameter to sort by the value of the "mymeta" field in descending order (so that matched posts appear first), and then by date in descending order (so that the most recent posts appear first within each group).
$query->set( 'orderby', array( 'meta_value' => 'DESC', 'date' => 'DESC' ) );
add_action( 'pre_get_posts', 'group_matched_posts_at_top' );
Les principaux changements sont les suivants :
-
J’ai utilisé le paramètre “taxonomy” pour spécifier la taxonomie à interroger.
-
J’ai utilisé le paramètre “opérateur” au lieu de “comparer” pour spécifier l’opérateur de comparaison (IN ou NOT IN).
-
J’ai ajouté le paramètre “field” avec une valeur de “slug” pour spécifier que nous comparons le terme slug (c’est-à-dire le champ “mymeta” du terme).
Ces modifications devraient permettre à la requête de fonctionner avec un champ méta terme/taxonomie, qui n’était pas pris en charge par ma solution précédente.