Untitled

mail@pastecode.io avatar
unknown
php
a year ago
1.1 kB
11
Indexable
Never
<div id="container">
  <?php
    $args = array(
      'post_type' => 'product',
      'posts_per_page' => -1
    );
    $query = new WP_Query( $args );
    if ( $query->have_posts() ) {
      while ( $query->have_posts() ) {
        $query->the_post();
        $search_query = isset( $_GET['search'] ) ? $_GET['search'] : '';
        ?>
          <div class="product">
            <h2><?php the_title(); ?></h2>
            <img src="<?php the_post_thumbnail_url(); ?>" alt="<?php the_title(); ?>">
            <p><?php the_content(); ?></p>
            <div class="product-rating"><?php echo get_product_rating( get_the_ID(), $search_query ); ?></div>
          </div>
        <?php
      }
    } else {
      echo "No products found.";
    }
    wp_reset_postdata();
    
    function get_product_rating( $product_id, $search_query ) {
      global $wpdb;
      $rating = $wpdb->get_var( "SELECT rating FROM {$wpdb->prefix}product_ratings WHERE product_id = {$product_id} AND comment LIKE '%{$search_query}%'" );
      return $rating ? $rating : 'No rating';
    }
  ?>
</div>