Ajax Load More Custom Query
-
Hi all,
I’m looking for a way to customize the query used in ‘chaplin_ajax_load_more’.
$query_args = json_decode( wp_unslash( $_POST['json_data'] ), true ); $ajax_query = new WP_Query( $query_args );
The goal is to exclude posts of a specific category, IFF they have already been placed on the front page, by a separate loop. Imagine a ‘Featured’ section with 3 of the latest posts from the ‘featured’ category. These 3 should be excluded from the main loop that posts the ‘Latest Stories’, as well as older stories from the ‘featured’ category once out of the ‘posts_per_page’ range.
In practice the featured stories should cycle back into the main loop of latest stories.
I’ve added the following to the child theme functions.php and front-page.php files, respectively.
Any help would be much appreciated.
Please let me know if you need any more info or clarification.
==================
functions.php
==================/* ----------------------------------------------------------------------------------------------- AJAX LOAD MORE Called in construct.js when the user has clicked the load more button --------------------------------------------------------------------------------------------------- */ if ( ! function_exists( 'chaplin_ajax_load_more' ) ) : function chaplin_ajax_load_more() { global $dd_dnd; $query_args = json_decode( wp_unslash( $_POST['json_data'] ), true ); $ajax_query = new WP_Query( $query_args ); // SET DND ARRAY FOR FEATURED POSTS $ajax_query->set( 'post__not_in', $dd_dnd ); $ajax_query->set( 'cat', array( 169 ) ); // Determine which preview to use based on the post_type $post_type = $ajax_query->get( 'post_type' ); // Default to the "post" post type for previews if ( ! $post_type || is_array( $post_type ) ) { $post_type = 'post'; } if ( $ajax_query->have_posts() ) : while ( $ajax_query->have_posts() ) : $ajax_query->the_post(); ?> <div class="grid-item"> HIDEO TEST TEXT <?php get_template_part( 'parts/preview', $post_type ); ?> </div><!-- .grid-item --> <?php endwhile; endif; wp_die(); } add_action( 'wp_ajax_nopriv_chaplin_ajax_load_more', 'chaplin_ajax_load_more' ); add_action( 'wp_ajax_chaplin_ajax_load_more', 'chaplin_ajax_load_more' ); endif;
==================
front-page.php
==================<?php get_header(); // DO NOT DUPLICATE LIST - FEATURED ARTICLES global $dd_dnd; $dd_dnd = array(); ?> <main id="site-content" role="main"> <?php $archive_title = ''; $archive_subtitle = ''; if ( is_search() ) { global $wp_query; /* Translators: %s = The search query */ $archive_title = sprintf( _x( 'Search: %s', '%s = The search query', 'chaplin' ), '“' . get_search_query() . '”' ); if ( $wp_query->found_posts ) { /* Translators: %s = Number of results */ $archive_subtitle = sprintf( _nx( 'We found %s result for your search.', 'We found %s results for your search.', $wp_query->found_posts, '%s = Number of results', 'chaplin' ), $wp_query->found_posts ); } else { $archive_subtitle = __( 'We could not find any results for your search. You can give it another try through the search form below.', 'chaplin' ); } } else { $archive_title = get_the_archive_title(); $archive_subtitle = get_the_archive_description( '<div>', '</div>' ); } // Check if we're hiding the archive header on the blog page $show_home_header = get_theme_mod( 'chaplin_show_archive_header_on_home', true ); if ( ( ! is_home() || is_home() && $show_home_header ) && ( $archive_title || $archive_subtitle ) ) : ?> <header class="archive-header section-inner"> <?php if ( $archive_title ) : ?> <h1 class="archive-title"><?php echo wp_kses_post( $archive_title ); ?></h1> <?php endif; ?> <?php if ( $archive_subtitle ) : ?> <div class="archive-subtitle section-inner thin max-percentage intro-text"><?php echo wp_kses_post( wpautop( $archive_subtitle ) ); ?></div> <?php endif; ?> </header><!-- .archive-header --> <?php endif; ?> <!-- START FEATURED STORIES SECTION --> <div class="posts posts-featured section-inner"> <h3 class="text-center">Featured Stories</h3> <!-- THE LOOP STARTS HERE --> <?php $dd_chaplin_featured_args = array( 'post_type' => 'post', 'cat' => array( 169 ), //show featured stories 'ignore_sticky_posts' => true, 'posts_per_page' => 3, ); $dd_chaplin_featured_post_query = new WP_Query($dd_chaplin_featured_args); if ($dd_chaplin_featured_post_query->have_posts()) : // CONFORM TO CUSTOMIZER GRID SETTINGS $post_grid_column_classes = chaplin_get_post_grid_column_classes(); // ADD TO EXCLUSION LIST global $dd_dnd; ?> <div class="posts-grid grid <?php echo $post_grid_column_classes; ?>"> <?php while ($dd_chaplin_featured_post_query->have_posts()) : $dd_chaplin_featured_post_query->the_post(); ?> <!-- ADD TO EXCLUSION LIST --> <?php $dd_dnd[] = get_the_ID(); ?> <div class="grid-item"> <?php get_template_part( 'parts/preview', get_post_type() ); ?> </div><!-- .grid-item --> <?php endwhile; ?> </div><!-- .posts-grid --> <?php endif; wp_reset_postdata(); ?> </div> <!-- END FEATURED STORIES SECTION --> <!-- START LATEST STORIES SECTION --> <div class="posts section-inner"> <?php // THE LOOP STARTS HERE global $dd_dnd; $dd_chaplin_mainfeed_args = array( 'post_type' => 'post', 'post__not_in' => $dd_dnd, 'posts_per_page' => 3, 'ignore_sticky_posts' => true, ); $dd_chaplin_mainfeed_post_query = new WP_Query($dd_chaplin_mainfeed_args); if ( $dd_chaplin_mainfeed_post_query->have_posts() ) : $post_grid_column_classes = chaplin_get_post_grid_column_classes(); ?> <h3 class="text-center">Latest Stories</h3> <div class="posts-grid grid load-more-target <?php echo $post_grid_column_classes; ?>"> <!-- NODE FOR POSTS TO LOAD UP WITH AJAX --> </div><!-- .posts-grid --> <?php elseif ( is_search() ) : ?> <div class="no-search-results-form"> <?php get_search_form(); ?> </div><!-- .no-search-results --> <?php endif; wp_reset_postdata(); ?> </div><!-- .posts --> <!-- END LATEST STORIES SECTION --> <?php get_template_part( 'pagination' ); ?> </main><!-- #site-content --> <?php get_footer(); ?>
- The topic ‘Ajax Load More Custom Query’ is closed to new replies.