Support » Plugin: Contextual Related Posts » AJAX loading of posts, related posts not displaying

  • Resolved jeroslav

    (@jeroslav)


    Hello. I really like your plugin. Works well already on one site.
    Now on this new site, I am loading the posts via AJAX. Now in the response function inside functions.php file I have inserted the function below:

    <?php if ( function_exists( 'echo_ald_crp' ) ) echo_ald_crp(); ?>

    Also tried with shortcode like this:

    echo do_shortcode('[crp limit="5" heading="1" cache="0"]');

    The div with id=”crp_related” gets displayed but no post content. I don’t even get any errors and I have set WP_DEBUG to true.

    Do you have any suggestions why the posts aren’t loading? πŸ™‚

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Author Ajay

    (@ajay)

    What’s the full source code? I’m not completely clear as to how this loading works on your site

    function cy_getContent_function(){
    
    	$post_id = $_POST['postContent'];
    	$post_author = get_post_field( 'post_author', $post_id );
    	
    	setPostViews($post_id);	
    
    	if( isset( $post_id ) ) {
    		$ajaxPost = get_post($post_id);
    	} else {
    		$args = array(
    			'orderby' => 'date', // we will sort posts by date
    			'order'	=> $_POST['date'] // ASC ΠΈΠ»ΠΈ DESC
    		);
    	}
    	
    
    		echo '<div class="news-content-byline">';
    		echo get_the_post_thumbnail($post_id);
    		echo '<span>' . get_the_author_meta('display_name', $post_author) .'</span>';
    		echo '<span class="close-news-content"><i class="fa fa-close"></i></span>';		
    		echo '<span class="date-published">Published on: ' . date('F j, Y', strtotime($ajaxPost->post_date)) . '</span>';
    		echo '</div>';
    		echo '<div class="news-content-content">';
    		echo '<h2>' . $ajaxPost->post_title . '</h2>';
    		echo '<p>' . $ajaxPost->post_content . '</p>';
    		echo '<a href="' . get_post_meta($post_id, 'wprss_item_permalink', true) .'" id="read-more" target="_blank">READ FULL ARTICLE</a>';
    		
    		
    		echo do_shortcode('[crp limit="5" heading="1" cache="0"]');
    		if ( function_exists( 'echo_ald_crp' ) ) echo_ald_crp(); 
    
    		wp_reset_postdata();
     
    	die();
    }

    This is the function that returns an AJAX response, I use it in the functions.php file.

    Should I be calling any JS function, for example new init or something? Or is it because the function is not used inside the loop and I am fetching the posts data by posts ID. Should I be passing the post ID? πŸ™‚

    • This reply was modified 1 year, 4 months ago by  jeroslav.
    • This reply was modified 1 year, 4 months ago by  jeroslav.
    Plugin Author Ajay

    (@ajay)

    Can you try echo get_crp( $post_id );

    Yup, just tried it. The result is the same, it outputs just the empty div with id=”crp_related”.

    I also did the var_dump(get_crp( $post_id)); and I got string(29) and then <div id="crp_related"> </div>. Which is this string with 29 characters. But nothing inside it.

    Plugin Author Ajay

    (@ajay)

    Are you in a position to debug? Might be worthwhile breaking down the code slightly.

    See the API code example. Idea would be to try it with get_crp_posts_id

    https://gist.github.com/ajaydsouza/968b24a052e858bf8926

    OK, just checked the plugin settings again. I had “When there are no posts, what should be shown?” checked to blank output. Now I’ve set it to “No related posts found” and it displays this text.

    So actually it doesn’t find related posts via AJAX functionallity.. Because I have single.php file that loads without AJAX (for example if the user goes directly to some specific post by inserting the post URL in the browser the related posts are displayed).

    So this “No related posts found” happens only when the new post is loaded dynamically with AJAX. So without single.php page “interaction” in this case.

    Plugin Author Ajay

    (@ajay)

    This could probably be because of the post ID not properly being passed through. Would require a bit of debugging to get there at different stages of the calculation.

    including a var_dump of $post_id or alternatively using $ajaxPost->post_ID

    TLDR; So basically I copied your example listed here: https://gist.github.com/ajaydsouza/968b24a052e858bf8926 and just modified the code where in $scores array with ‘postid’ assigning to value of $ajaxPost->ID and it works πŸ™‚ The posts are now fetched and displayed πŸ˜€

    Ok so I found that:

    • $post_id was returning ID as a string.
    • $ajaxPost->ID was returning ID as an integer.
    • If I var_dump $ajaxPost I get a successful WP_Post object with all correctly inserted post data. β€œID” being the integer of 7857 for example. So the ID was being fetched correctly.

    Then I tried: var_dump( get_crp_posts_id( $ajaxPost->ID) ); but it returned bool(false).

    But if I do: var_dump( get_crp_posts_id(array( 'postid '=> $ajaxPost->ID ) ); it returns succesfully the array(4) of four related posts (their data wrapped in Objects) and their respective ID’s.

    So basically I copied your example listed here: https://gist.github.com/ajaydsouza/968b24a052e858bf8926 and just modified the code where in $scores array with ‘postid’ assigning to value of $ajaxPost->ID and it works πŸ™‚ The posts are now fetched and displayed πŸ˜€

    The full code regarding working related posts is now like this:

    if ( function_exists( 'get_crp_posts_id' ) ) {
    		$scores = get_crp_posts_id( array(
    			'postid' => $ajaxPost->ID,
    			'limit' => 4 
    		) );
    		$posts = wp_list_pluck( (array) $scores, 'ID' );
    		$args = array(
    			'post__in' => $posts,
    			'posts_per_page' => 4,
    			'ignore_sticky_posts' => 1 
    		);
    		$my_query = new WP_Query( $args );
    		if ( $my_query->have_posts() ) {
    			echo '<ul>';			
    			while ( $my_query->have_posts() ) {
    				$my_query->the_post();
    				echo '<li>';
    				echo '<a href="' . get_permalink( get_the_ID() ) . '">';
    				echo '<img src="' . get_the_post_thumbnail_url() . '" />';
    				the_title();
    				echo '</a>';
    				echo '</li>';
    				wp_reset_postdata();
    			}
    			echo '</ul>';
    		} else {
    		}
    		wp_reset_query();
    	}
    
    		wp_reset_postdata();
     
    	die();

    Thanks for your support & I’ll make sure this plugin gets 5 star rating from me πŸ™‚

    • This reply was modified 1 year, 4 months ago by  jeroslav. Reason: Reply marked as resolved
    Plugin Author Ajay

    (@ajay)

    Thank you for patiently working through and fixing this πŸ™‚

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘AJAX loading of posts, related posts not displaying’ is closed to new replies.