Do not query similar content using custom meta on Ajax WordPress

Question

I got the relelated content from costum meta using it and it works fine but when I use Ajax it query "relation" applies it incorrectly

add_action( 'wp_enqueue_scripts', 'related_video_scripts' );
function related_video_scripts() {
  wp_localize_script( 'scripts', 'videolist', array(
    'ajax_url' => admin_url( 'admin-ajax.php' )
  ));
}

add_action( 'wp_ajax_nopriv_all_video', 'all_video' );
add_action( 'wp_ajax_all_video', 'all_video' );

function all_video() {

  if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { 

    global $post;  

    $html = '<ul id="all-video" class="video-box row-1">';

    $artist = get_post_meta(get_the_ID(), 'name', true);
    $artist1 = get_post_meta(get_the_ID(), 'name1', true);
    $artist2 = get_post_meta(get_the_ID(), 'name2', true);

    $relate_video = get_posts( 
        array(
        //'numberposts' => -1,
        'post_type'   => 'post',
        //'post_format' => 'post-format-video',
        'post_status' => 'publish',
        'meta_key' => 'name',
        'order' => 'DESC',
        'orderby' => 'meta_value',
        'posts_per_page' =>'6',
        'post__not_in' => array( $post->ID ),
        'meta_query'    => array(
            'relation'      => 'OR',
                array(
                    'key'       => 'name',
                    'value'     => $artist,
                    'compare'   => 'LIKE'
                ),
                array(
                    'key'       => 'name',
                    'value'     => $artist1,
                    'compare'   => '='
                ),
                array(
                    'key'       => 'name',
                    'value'     => $artist2,
                    'compare'   => '='
                )
            )
        )
    );

    if( $relate_video ){
        foreach( $relate_video as $post ) {

            $title = $post->name.' - '.$post->song;

            if ( empty($post->name) || empty($post->song) ) {
                $title = $post->post_title;
            }

            $images = get_the_post_thumbnail( $post->ID, 'medium', array('class' => '', 'alt'=> $title) );

            $html .= '<li class="video-post row-6"><span class="icon-play"></span><a class="data-audio" href="'.get_permalink($post->ID).'">'.$images.$title.'</a></li>';

        }
        wp_reset_query();
        wp_reset_postdata();
    }

    echo $html .'</ul>';

    die();
  }
}
0
Hosein Farahmand 3 weeks 0 Answers 1 views 0

Leave an answer