Skip to main content

Get related post or products based on meta key and values through wordpress function



Get related post or products based on meta key and values through wordpress function
Solution 1
 add this function into your custom plugin or function.php file
 
function print_event_venues( $mix_venue )
{  
    $args = array(
        'numberposts' => -1,
        'post_type'   => 'post',
        'post_status' => 'publish',
        'meta_key' => 'event_venue',
        'meta_value' => $mix_venue
        );
    $posts = get_posts( $args );

    // DO NOTHING
    if( !$posts )
        return;

    foreach( $posts as $post )
    {
        // DO YOUR HTML
        echo $post->post_title;
    }
}


then you can call it this function from your php file

<?php echo print_event_venues( $mix_venue ); //pass meta value ?>


Solution 2--
You can also get related post or products based on meta key and values by this function ..just paste this in your function.php and use shortcode to print results
function get_post_data_with_details()
{  
  $meta_catagory = get_post_meta(get_the_ID(), "catagory" , true);


    $args = array(
        'post_type'   => 'product',
        'post_status' => 'publish',
        'meta_key' => 'catagory',
        'meta_value' => $meta_catagory,
  'showposts'  => 5,
        'orderby'     => 'rand'
        );
    $posts = get_posts( $args );

    // DO NOTHING
    if( !$posts )
        return;

    foreach( $posts as $post )
    {
       $catagroy_title = $product_details['catagory'];
       $meta = get_post_meta( $post->ID );
     $product_meta_details="";
     foreach ($meta as $key => $metavalue) {
      $product_meta_details[$key] = $metavalue[0];
     }
    
        $product_title = $post->post_title;
         if (strlen($product_title) > 35) {
          $product_title = substr($product_title, 0, 35);
        $product_title = substr($product_title, 0, strrpos($product_title, ' ')) . " ...";
      }
        ?>
      <div class="col-sm-3 product_wrapper bottom_pro_wrap">
      <div class="img_pro_wrap">
        <a href="<?php echo $post->guid; ?>"><img  class="default-img-class" src="<?php echo $product_meta_details['image_large']  ?>" alt="<?php the_title(); ?>" height="150px" width="100%" /></a>
      </div>
      <div class="title_wrapper bottom_title_wrap">
        <a class="nodecor" href="<?php echo $post->guid; ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?> "><?php echo $product_title; ?></a>
        <div class="manu_title"><?php if(!empty($product_meta_details['manufacturer'])) { echo $product_meta_details['manufacturer']; } ?></div>
      </div>
      <p class="price_wrapper">$<?php echo $product_meta_details['price'];  ?></p>
  </div>

   <?php
    }
}

add_shortcode('getrelatedproducts', 'get_post_data_with_details');


-----------------------------------------------------------
Solution 3

function get_all_companies_selling( $product_id ){
    global $wpdb;
    $sql = "select post_id from " . $wpdb->prefix . "postmeta where
        meta_key = 'prefix_products' &&
        meta_value like '%%%s%%'";

    $product_id = 's:' . strlen( $product_id ) . ':"' . (int) $product_id . '";';
    $sql = $wpdb->prepare( $sql, $product_id );
    $res = $wpdb->get_results( $sql );

    return $res;
}

get_all_companies_selling( 331  );


Comments

Post a Comment