Advertisement
Guest User

Untitled

a guest
Jun 9th, 2017
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.91 KB | None | 0 0
  1. <?php
  2. function wpforum218064_recommend_posts() {
  3.     global $post;
  4.  
  5.     if ( ! is_single() ) {
  6.         return;
  7.     }
  8.  
  9.     $html      = '';
  10.     $meta_key  = 'products-cate';
  11.     $products  = get_post_meta( $post->ID, $meta_key, true );
  12.     $meta_args = array(
  13.         'relation' => 'OR',
  14.     );
  15.  
  16.     if ( ! empty( $products ) ) {
  17.         foreach ( $products as $product ) {
  18.             $product     = (int) $product;
  19.             $meta_args[] = array(
  20.                 'key'     => $meta_key,
  21.                 'value'   => '"' . $product . '"',
  22.                 'compare' => 'LIKE',
  23.             );
  24.         }
  25.     }
  26.  
  27.     $args = array(
  28.         'post_type'    => array( 'post', 'page' ),
  29.         'meta_query'   => $meta_args,
  30.         'post__not_in' => (array) $post->ID,
  31.     );
  32.     $query = new WP_Query( $args );
  33.     $html .= '<ul>';
  34.  
  35.     while ( $query->have_posts() ) {
  36.         $query->the_post();
  37.         $html .= '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
  38.     }
  39.  
  40.     $html .= '</ul>';
  41.  
  42.     wp_reset_postdata();
  43.  
  44.     echo $html;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement