php - How can i limit the number of posts to the most recent 6 in my query?

I have a crashing jQuery slider that i have been asked to fix. Currently it is loading 128 slides (causing crashed of co

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>

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>

Share Improve this question asked Jul 22, 2019 at 18:19 Ben BlueBen Blue 1171 silver badge8 bronze badges 4
  • 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 Commented Jul 22, 2019 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. – Ben Blue Commented Jul 22, 2019 at 18:52
  • @mnr How do i ensure this is the 6 most recent posts? – Ben Blue Commented Jul 22, 2019 at 18:58
  • By default, posts are sorted by publication date (you set this explicitly - 'orderby' => 'date'), from newer to older. – nmr Commented Jul 22, 2019 at 19:55
Add a comment  | 

1 Answer 1

Reset to default 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745304497a4621629.html

相关推荐

  • php - How can i limit the number of posts to the most recent 6 in my query?

    I have a crashing jQuery slider that i have been asked to fix. Currently it is loading 128 slides (causing crashed of co

    16小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信