• aryanrajseo

    (@aryanrajseo)


    Hi,

    This plugin is amazing. I wonder if it offers the load more feature for a custom wp_query have the pagination active. Here is the basic custom wp_query that output post list.

    // Shortcode [dy_ajax_posts]
    if ( ! function_exists('dy_ajax_posts_shortcode') ) {
        function dy_ajax_posts_shortcode( $atts ){
    
            global $post;
    		global $wp_query;
    
            ob_start();
    
    		if ( is_front_page() ) {
    			$paged = ( get_query_var( 'page' ) ) ? get_query_var( 'page' ) : 1;
    		} else {
    			$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    		}
    
            $args = array(
                'post_type'         =>  'post',
                'posts_per_page'    =>  2, 
    			'paged' => $paged
            );
    
        $query = new WP_Query($args);
            
     // The Loop
     if ($query->have_posts() ) :
    
    	echo '<div class="post-container">';
    
        while ( $query->have_posts() ) : $query->the_post();
            
            // Do Stuff
            $title = get_the_title();
            $permalink = get_the_permalink();
         
            echo '<article class="post article"><a href="'.$permalink.'">'.$title.'</a></article>';
    
    		
            endwhile;
    
    		if ( is_front_page() ) {
    			echo '<div class="pagination" role="navigation" aria-label="Pagination">';
    			echo paginate_links(array(
    				'current'=>max(1,get_query_var('page')),
    				'total'=>$query->max_num_pages,
    				
    			));
    			echo '</div>';
    		} else {
    			echo '<div class="pagination" role="navigation" aria-label="Pagination">';
    			echo paginate_links(array(
    				'total' => $query->max_num_pages,
    			));
    			echo '</div>';
    		}
    
        wp_reset_postdata();
    endif;
    
    	echo '</div>';
    
        $html = ob_get_clean();
            return $html;
        }
    }
    
    add_shortcode('dy_ajax_posts', 'dy_ajax_posts_shortcode');
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Darren Cooney

    (@dcooney)

    Hi @aryanrajseo
    Thank you ??

    The plugin does offer the ability to pass a custom query using the alm_query_args filter.
    https://connekthq.com/plugins/ajax-load-more/docs/filter-hooks/#alm_query_args

    However, you wouldn’t want to be making modifications to the paged value you can do that with the offset parameter.

    Have a look and let me know what you think.

    Thread Starter aryanrajseo

    (@aryanrajseo)

    Thank you for quick answer. I looked at the doc but i am not confident enough to make it work. I tried but that doesn’t seems to work. Could you highlight the code that need to be fixed? or a sample query using the alm_query_args filter. Thanks. Also, the code on the doc site seems to have invisible characters giving error in frontend. VScode show “Exclude U+00a0 (invisible character) from being highlighted”

    Here what i have for now.

    // Shortcode [dy_ajax_posts]
    if ( ! function_exists('dy_ajax_posts_shortcode') ) {
        function dy_ajax_posts_shortcode( $args ){
    
            global $post;
    
            ob_start();
    
    		if ( is_front_page() ) {
    			$paged = ( get_query_var( 'page' ) ) ? get_query_var( 'page' ) : 1;
    		} else {
    			$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    		}
    		
            $args = array(
                'post_type'         =>  'post',
                'posts_per_page'    =>  2, 
    			'paged' => $paged
            );
    
        $query = new WP_Query($args);
            
     // The Loop
     if ($query->have_posts() ) :
    
        while ( $query->have_posts() ) : $query->the_post();
            
            // Do Stuff
            $title = get_the_title();
            $permalink = get_the_permalink();
         
            echo '<article class="post article"><a href="'.$permalink.'">'.$title.'</a></article>';
    		
            endwhile;
    
        wp_reset_postdata();
    endif;
    
        $html = ob_get_clean();
            return $html;
        }
    }
    
    add_shortcode('dy_ajax_posts', 'dy_ajax_posts_shortcode');
    
    function my_movie_listing( $args, $id ) {
    	$args['post_type'] = 'post';
    
    }
    
    add_filter( 'alm_query_args_movie_listing', 'my_movie_listing', 10, 2);
    
    add_filter( 'alm_query_args_dy_ajax_posts', 'dy_ajax_posts_shortcode');
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Ajax Load more with custom wp_query?’ is closed to new replies.