• Resolved alejuu

    (@alejuu)


    Hi, I’m using this tutorial to accomplish related posts without a plugin https://wpcrumbs.com/2017/03/how-to-display-related-posts-without-a-plugin/#comment-5948 , and I’m trying to add ajax load more to display even more podcasts.

    So I’m adding this code to my functions

    /**
     * Related posts
     *
     * @global object $post
     * @param array $args
     * @return
     */
    function wcr_related_posts($args = array()) {
        global $post;
    
        // default args
        $args = wp_parse_args($args, array(
            'post_id' => !empty($post) ? $post->ID : '',
            'taxonomy' => 'category',
            'limit' => 3,
            'post_type' => !empty($post) ? $post->post_type : 'post',
            'orderby' => 'date',
            'order' => 'DESC'
        ));
    
        // check taxonomy
        $registered_taxonomies = get_taxonomies();
    
        if (!in_array($args['taxonomy'], $registered_taxonomies)) {
            return;
        }
    
        // post taxonomies
        $taxonomies = wp_get_post_terms($args['post_id'], $args['taxonomy'], array('fields' => 'ids'));
    
        if (empty($taxonomies)) {
            return;
        }
    
        // query
        $related_posts = get_posts(array(
            'post__not_in' => (array) $args['post_id'],
            'post_type' => $args['post_type'],
            'tax_query' => array(
                array(
                    'taxonomy' => $args['taxonomy'],
                    'field' => 'term_id',
                    'terms' => $taxonomies
                ),
            ),
            'posts_per_page' => $args['limit'],
            'orderby' => $args['orderby'],
            'order' => $args['order']
        ));
    
        include( locate_template('relatedposts.php', false, false) );
    
        wp_reset_postdata();
    }

    then I include this in the single.php like this

    <?php wcr_related_posts(); ?>

    and add ajax load more like this

    if(!empty($related_posts)){
       $cat = get_query_var('cat');
       $category = get_category ($cat);
       echo do_shortcode('[ajax_load_more container_type="div" css_classes="flex-box-row flex-wrap" repeater="template_2" post_type="post" posts_per_page="3" post_format="standard" category="'.$category->slug.'" cache="true" cache_id="cache-'.$category->slug.'" offset="6" pause="true" transition_container="false" images_loaded="true"]');
    }
    ?>

    And this brings the posts from all categories, not related to the post categories.

    So my question is, do you think there is a way I can change the $variable to import the related_posts categories.

    Thanks

    • This topic was modified 8 years, 11 months ago by alejuu.
    • This topic was modified 8 years, 11 months ago by alejuu.
    • This topic was modified 8 years, 11 months ago by alejuu.
    • This topic was modified 8 years, 11 months ago by alejuu.
    • This topic was modified 8 years, 11 months ago by alejuu.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Darren Cooney

    (@dcooney)

    Hi @alejuu,
    Why do you mean by related post categories? Are you referring to related posts by category?

    With that related post script, I’m wondering if it’s possible to return post ids and then pass them to the post__in shortcode parameter.

    Would that work?

    Thread Starter alejuu

    (@alejuu)

    Hi @dcooney
    Yes, I mean that, related posts by category. Also, I wonder the same and I think that should work. The problem is that I’m not that good in php, so whatever I try it doesn’t work, or I’m able to load posts but not related to the post categories.
    Thanks!

    Plugin Author Darren Cooney

    (@dcooney)

    Yea, this is tough if you don’t have much PHP knowledge.

    What i think you need to do is update the wcr_related_posts function to return Post IDs only and then pass them to Ajax Load More…

    
    $related_posts = get_posts(array(
      'post__not_in' => (array) $args['post_id'],
      'post_type' => $args['post_type'],
      'tax_query' => array(
          array(
              'taxonomy' => $args['taxonomy'],
              'field' => 'term_id',
              'terms' => $taxonomies
          ),
      ),
      'posts_per_page' => $args['limit'],
      'orderby' => $args['orderby'],
      'order' => $args['order']
    ));
    
    $post_in = array();
    foreach ( $related_posts as $post ){ 
       setup_postdata( $post );
       $post_in[] = $post->ID; // Store post IDs
    }
    wp_reset_postdata();
    
    echo do_shortcode('[ajax_load_more post__in="'.implode(',', $post_in).'"]');
    

    Have a look and see what I did.

    Thread Starter alejuu

    (@alejuu)

    Thanks @dcooney, this is working! Awesome!

    • This reply was modified 8 years, 11 months ago by alejuu.
    • This reply was modified 8 years, 11 months ago by alejuu.
    Plugin Author Darren Cooney

    (@dcooney)

    Wow nice one!

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Ajax Load More for Related posts’ is closed to new replies.