Support » Plugin: WordPress Infinite Scroll - Ajax Load More » Get Related Posts by Tag if Tag is Empty Get Posts by Category

  • Resolved turndriverside

    (@turndriverside)


    I’m using Ajax Load More, and I’m trying to get related posts by tag. Which works:

    $terms = wp_get_post_tags($post->ID); // get current categories
    $term_array = []; // Create empty category array
    
    foreach( $terms as $term ) { // Loop founf categories
      $term_array[] = $term->slug;
    }
    
    echo do_shortcode('[ajax_load_more tag="'. implode(",", $term_array) .'" post__not_in="' . $post->ID . '" post_type="post" posts_per_page="3" scroll="false" progress_bar="true" progress_bar_color="blue" images_loaded="true" button_label="Load More" css_classes="related-posts-container" button_loading_label="Loading More" container_type="div" ]');

    But when I try to implement a feature where I get posts by category when that tag is empty it crashes on me. Any idea what I might be doing wrong?

    function getRelatedPostsByTag(){
    // Related Posts
    $terms = wp_get_post_tags($post->ID); // get current categories
    $term_array = []; // Create empty category array
    
    foreach( $terms as $term ) { // Loop founf categories
      $term_array[] = $term->slug;
    }
    
    $args = array(
        'posts_per_page'    => 1,
        'post__not_in'  => $post->ID,
        'tag'               => implode(",", $term_array),
        'post_type'         => 'post'
    );
    $related_posts = get_posts( $args );
    
    $query = 'tag="'. implode(",", $term_array) .'"';
    
    if (count($related_posts) > 1) {
    return $query;
    }
    else {
    wp_reset_postdata();
    getRelatedPostsByCategory();
    }
    }
    
    function getRelatedPostsByCategory(){
    
    $categories = wp_get_post_categories($post->ID);
    $categories_array = [];
    
    foreach ($categories as $category) {
        $categories_array[] = $category->CatSlug;
    }
    $query = 'category="'. implode(",", $categories_array) .'"';
    
    return $query;
    }
    
    wp_reset_postdata();
    
    echo do_shortcode('[ajax_load_more '. getRelatedPostsByTag() .' post__not_in="' . $post->ID . '" post_type="post" posts_per_page="3" scroll="false" progress_bar="true" progress_bar_color="blue" images_loaded="true" button_label="Load More" css_classes="related-posts-container" button_loading_label="Loading More" container_type="div" ]');
Viewing 1 replies (of 1 total)
  • This is the code that ended up working, thank you to Darren for helping me troubleshoot it!

    
    // Related Posts by tag
    $terms = wp_get_post_tags($post->ID); // get current categories
    $term_array = []; // Create empty category array
    
    foreach( $terms as $term ) { // Loop founf categories
    $term_array[] = $term->slug;
    }
    $query = '';
    
    $args = array(
    'posts_per_page' 	=> 1,
    'post__not_in' 	=> $post->ID ,
    'tag' 				=> implode(",", $term_array),
    'post_type' 		=> 'post'
    );
    $related_posts = get_posts( $args );
    if ($related_posts) { // Tags
    
    $query = ' tag="'. implode(",", $term_array) .'"';
    
    } else {
    
    $categories = wp_get_post_categories($post->ID);
    
    if($categories){
    $categories_array = [];
    foreach ($categories as $category) {
    $cat = get_term( $category );
    $categories_array[] = $cat->slug;
    }
    if(!empty($categories_array)){
    $query = ' category="'. implode(",", $categories_array) .'"';
    }
    }
    
    }
    
    echo do_shortcode('[ajax_load_more'. $query .' post__not_in="' . $post->ID . '" post_type="post" posts_per_page="3" scroll="false" progress_bar="true" progress_bar_color="blue" images_loaded="true" button_label="Load More" css_classes="related-posts-container" button_loading_label="Loading More" container_type="div" ]');
    
Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.