web-dev-qa-db-fra.com

Inclusion de méta personnalisé dans la requête posts_where

J'ai regardé autour de moi pour voir comment faire cela, mais les solutions que j'ai trouvées ne semblent pas fonctionner pour moi.

Je suis en train de lancer une AJAX avancée recherche de certains types de publication Wordpress personnalisée. Je suis assez loin et tout fonctionne très bien. Sauf que maintenant je suis bloqué sur la façon d'inclure les champs personnalisés du type d'article personnalisé dans ma recherche (ainsi que le contenu et le titre). Je veux juste ajouter à l'instruction SQL faire la recherche, mais je ne suis pas très versé dans SQL.

J'ai essayé

Voici ce que j'ai jusqu'à présent dans le fichier functions.php pour interroger la base de données une fois le terme de recherche envoyé par l'appel AJAX:

function display_all_products() {

        global $term;
        $term = trim(strip_tags($_POST['s']));
            if(!empty($_POST['s']))
            {
                add_filter( 'posts_where' , 'product_posts_where' );
            }
            $args = array( 
                'posts_per_page' => 50,
                'paged' => 1, 
                'post_type' => 'product', 
                'order' => 'DESC',
            );

            $the_query = new WP_Query( $args ); 

            if($the_query->have_posts()) {
                while ( $the_query->have_posts() ) {
                    $the_query->the_post();

                    $pid = get_the_ID();
                    $refno = get_post_meta( $pid, 'refno', true );
                    $yr = get_post_meta( $pid, 'yr', true );
                    $pieces = get_post_meta( $pid, 'pieces', true );
                    $figures = get_post_meta( $pid, 'figures', true );
                    ?>
                    <div class="my_box3">
                        <h3><?php the_title(); ?></h3>
                        <div class="padd10"><?php the_post_thumbnail(); ?></div>
                        <div class="padd10">
                            <ul>
                                <li> <?php _e('Referance Number', 'AuctionTheme');?>: <?php  echo $refno; ?></li>
                                <li> <?php _e('Year', 'AuctionTheme'); ?>: <?php echo $yr; ?></li>
                                <li> <?php _e('Pieces', 'AuctionTheme'); ?>: <?php echo $pieces; ?></li>
                                <li> <?php _e('Figures', 'AuctionTheme'); ?>: <?php echo $figures; ?></li>
                            </ul>
                    </div>

                              <?php}
            }
            wp_reset_query();
        die();
}

add_action('wp_ajax_show_all_products', 'display_all_products');
add_action('wp_ajax_nopriv_show_all_products', 'display_all_products');

function product_posts_where( $where ) {

        global $wpdb, $term;
        $searchTerms = explode(' ', $term);

        $i = 1;
        $where .=" AND (";

        foreach ($searchTerms as $Word) {
            if($i === 1) {
                $where .= " ({$wpdb->posts}.post_title LIKE '%$Word%' OR {$wpdb->posts}.post_content LIKE '%$Word%')";
            } else {
                $where .= " OR ({$wpdb->posts}.post_title LIKE '%$Word%' OR {$wpdb->posts}.post_content LIKE '%$Word%')";
            }
            $i++;
        }

        $where .=")";

    return $where;
}

Ajouter ceci à la fonction product_posts_where fait échouer tout et supprime les données des champs personnalisés!

$where .= " OR ($wpdb->postmeta.meta_key = 'refno' AND $wpdb->postmeta.meta_value = '%$Word%')";
1
Ralphonz

Eh bien, j'ai réussi à résoudre ce genre de ...

La solution consistait à utiliser des zones de saisie de recherche distinctes pour chaque champ personnalisé faisant l'objet de la recherche.

Voici ma solution de travail si quelqu'un tombe jamais sur cela.

function display_all_products() {

    global $term;
    $term = trim(strip_tags($_POST['s']));
    $refnoterm = trim(strip_tags($_POST['ref']));
    $yrterm = trim(strip_tags($_POST['yr']));

    if(!empty($_POST['s']) || !empty($_POST['ref']) || !empty($_POST['yr'])) {

            if(!empty($_POST['s']))
            {
                add_filter( 'posts_where' , 'product_posts_where' );
            }

            if(!empty($_POST['ref']))
            {
                $refno_meta = array(
                    'key' => 'refno',
                    'value' => $refnoterm,
                    //'type' => 'numeric',
                    'compare' => 'LIKE'
                );
            }

            if(!empty($_POST['yr']))
            {
                $yr_meta = array(
                    'key' => 'yr',
                    'value' => $yrterm,
                    //'type' => 'numeric',
                    'compare' => 'LIKE'
                );
            }

        $meta_query = array('relation' => 'OR',);

        array_Push($meta_query, $refno_meta);
        array_Push($meta_query, $yr_meta);

            $args = array( 
                'posts_per_page' => 50,
                'paged' => 1, 
                'post_type' => 'product', 
                'order' => 'DESC',
                'meta_query' => $meta_query,
            );

            $the_query = new WP_Query( $args );

            remove_filter('posts_where', 'product_posts_where');


            if($the_query->have_posts()) {
                while ( $the_query->have_posts() ) {
                    $the_query->the_post();

                    $pid = get_the_ID();
                    $refno = get_post_meta( $pid, 'refno', true );
                    $yr = get_post_meta( $pid, 'yr', true );
                    $pieces = get_post_meta( $pid, 'pieces', true );
                    $figures = get_post_meta( $pid, 'figures', true );

                    $categories = get_the_terms( $pid, 'product_cat' );

                    ?>

                    <div class="my_box3">
                        <h3><?php the_title(); ?></h3>
                        <div class="padd10"><?php the_post_thumbnail(); ?></div>
                        <div class="padd10">
                            <ul>
                                <li> <?php _e('Referance Number', 'AuctionTheme');?>: <?php  echo $refno; ?></li>
                                <li> <?php _e('Year', 'AuctionTheme'); ?>: <?php echo $yr; ?></li>
                                <li> <?php _e('Pieces', 'AuctionTheme'); ?>: <?php echo $pieces; ?></li>
                                <li> <?php _e('Figures', 'AuctionTheme'); ?>: <?php echo $figures; ?></li>
                                <li> <?php _e('Category and Theme'); ?>:
                                    <?php
                                    foreach( $categories as $category) {
                                        echo $category->name . ', ';
                                    }
                                    ?>
                                </li>
                            </ul>
                        <label for="product">Select Product: <input type="radio" name="product" value="<?php the_ID(); ?>" /></label>
                    </div>

                    <?php   
                }
            } else {
                _e('There are no products that match your search, please try again.', 'AuctionTheme');
            }

            wp_reset_query();
            //wp_reset_postdata();

    } else {
        _e('You must enter something in at least one search box or select a product using the dropdown boxes and try again', 'AuctionTheme');
    }

    die();
}

add_action('wp_ajax_show_all_products', 'display_all_products');
add_action('wp_ajax_nopriv_show_all_products', 'display_all_products');

function product_posts_where( $where ) {

        global $wpdb, $term;
        $searchTerms = explode(' ', $term);

        $i = 1;
        $where .=" AND (";

        foreach ($searchTerms as $Word) {
            if($i === 1) {
                $where .= " ({$wpdb->posts}.post_title LIKE '%$Word%' OR {$wpdb->posts}.post_excerpt LIKE '%$Word%')";
            } else {
                $where .= " OR ({$wpdb->posts}.post_title LIKE '%$Word%' OR {$wpdb->posts}.post_excerpt LIKE '%$Word%')";
            }
            $i++;
        }

        $where .=")";

    return $where;
}
2
Ralphonz