suchen

Heim  >  Fragen und Antworten  >  Hauptteil

Stellen Sie die Beziehung „OR“ für meta_query ORtax_query ein

Ich versuche, mit WP_Query eine Abfrage zu erstellen, die ein meta_query-Element und ein tax_query-Element enthält. Noch wichtiger ist, dass ich keine Ergebnisse finden möchte, die beide Bedingungen (und die AND-Klausel) erfüllen, sondern Ergebnisse, die die eine oder andere davon erfüllen (OR-Klausel).

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

$loop = new WP_Query( array(

            'post_type' => 'tickets',

            'paged' => $paged,

            'order'     => $order,

            'orderby'   => $orderby,

                'meta_query' => array(

                    array(

                    'key' => 'tps_email', // Use 'key' for author

                    'value' => 'web@data.com', // Replace with the author ID you want to query

                    'compare' => 'LIKE', // Use '=' to match the author ID

                    ), 

                ), 

                'tax_query' => array(

                    array(

                    'taxonomy' => 'topic', // Replace with your custom taxonomy name

                    'field' => 'slug', // You can use 'id', 'slug', or 'name' depending on how you want to identify the term

                    'terms' => 'housekeeping', // Replace with the slug of the specific term you want to query

                    ),

                ),

            )

            );

Ich akzeptiere diese Ausgabe

P粉101708623P粉101708623334 Tage vor1586

Antworte allen(1)Ich werde antworten

  • P粉099000044

    P粉0990000442024-04-05 00:33:11

    以下是如何在meta_query和tax_query之间设置“OR”关系:

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    $args = array(

       'post_type' => 'tickets',

       'paged' => $paged,

       'order'     => $order,

      'tax_query' => array(

        'relation' => 'OR', // Set the relationship to OR

        array(

            'taxonomy' => 'topic',

            'field' => 'slug',

            'terms' => 'housekeeping',

        ),

        array(

            'taxonomy' => 'your_taxonomy_2',

            'field' => 'slug',

            'terms' => 'term_slug_2',

        ),

      ),

      'meta_query' => array(

        'relation' => 'OR', // Set the relationship to OR

        array(

            'key' => 'tps_email',

            'value' => 'web@data.com',

            'compare' => '='

        ),

        array(

            'key' => 'custom_field_2',

            'value' => 'custom_value_2',

            'compare' => '='

        ),

      ),

    );

    Antwort
    0
  • StornierenAntwort