0

I have a crashing jQuery slider that i have been asked to fix. Currently it is loading 128 slides (causing crashed of course), but we only want to see 6.

I tried using jQuery to remove() 7th and higher li elements, but the slider still loaded empty slides for those removed. I think the best solution is in the template code anyway.

These slides are pulled from posts who have a checkbox created from Advanced Custom Fields named "Featured Item". Asking or advising the client to uncheck the posts when they don't want them displayed is not an option.

Can you help me change the code to only pull the 6 most recent? Also, i would love to also have a script to uncheck that box right now for items 7 and above.

Thank you!

/*
 * Template name: Blog Landing
 */
global $faar_opt, $wp_query;

$acf_fields = get_fields();
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

$categories = get_field('categories_of_blog', $post->id);
// pretty sure this is what i need to modify, as it has 'featured_item' (the checkbox)
$args = array(
    'post_type' => 'post',
    'posts_per_page' => 8,
    'category__in' => $categories,
    'meta_query' => array(
        array(
            'key' => 'featured_item',
            'value' => '1'
        )
    ),
);
$related_posts = new WP_Query($args);

$args = array(
    'post_type' => 'post',
    'posts_per_page' => 4,
    'paged' => $paged,
    'orderby' => 'date',
    'category__in' => $categories
);

if(isset($wp_query->query_vars['blog_tag'])){
    $args['tag'] = $wp_query->query_vars['blog_tag'];
    $current_tag = get_term_by('slug', $wp_query->query_vars['blog_tag'], 'post_tag');
}

if(isset($wp_query->query_vars['blog_author'])){
    $current_author = get_user_by('login', urldecode($wp_query->query_vars['blog_author']));
    $args['author'] = $current_author->ID;
}

$all_posts = new WP_Query($args);

// each li is a slide, which is pulling 128, and needs to only be most recent 6
<ul class="slides blog-slides">
  <?php 
    while($related_posts->have_posts()): $related_posts->the_post();
    $author = get_user_by('id', $post->post_author);
   ?>
    <li>
     // slide content (removed for simplicity. Tell me if you need it.
    </li>
        <?php endwhile; wp_reset_query(); ?>
    </ul>

  • Comment $related_posts = new WP_Query($args); line, set 'posts_per_page' => 6, and belowe ul tag replace all $related_posts-> to $all_posts->. – nmr Jul 22 at 18:29
  • thank you - i just found out that the staging site and production site are out of sync, and the production site had 'posts_per_page' => -1,`. Changing that to 6 fixed it! I didn't get to try your solution, but thank you. – bluedimensional Jul 22 at 18:52
  • @mnr How do i ensure this is the 6 most recent posts? – bluedimensional Jul 22 at 18:58
  • By default, posts are sorted by publication date (you set this explicitly - 'orderby' => 'date'), from newer to older. – nmr Jul 22 at 19:55
0

After i posted the question, i was told the staging site which i was working on, and the production site's code was out of sync.

The answer ended up being simple: 'posts_per_page' => 8, (which was actually working but was out of sync with repos / server) was actually set to 'posts_per_page' => -1, which i changed to 'posts_per_page' => 6, and that did it!

And for pulling the most recent, i didn't realize it does by default, which i found that answer here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.