I have the following code working fine in my theme:

<!-- Modify query object -->
<?php query_posts('showposts=5&post_type=news'); ?>

<!-- Start the Loop -->
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

        <a href="<?php the_permalink(); ?>">
                if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
                <?php the_title(); ?>

<!-- Stop The Loop -->
 <?php endwhile; else: ?>
 <p>Sorry, no posts matched your criteria.</p>
 <?php endif; ?>

<!-- Reset the Loop. -->
<?php rewind_posts(); ?>

I need to the number of posts to be based on a custom field.

I am not sure how to go about doing this.

My set-up: Page template: front-page.php

Homepage: set to the 'home' Page

Custom field: Custom field is in the 'Home' Page. I am using ACF plugin for custom fields


UPDATE: - screenshots included of my setup

Edit screen of the 'Home' Page Site settings screen Custom field setup using ACF plugin

2 Answers 2


Found an answer to my question using the ACF plugin get_field function (http://www.advancedcustomfields.com/resources/functions/get_field/) to retrieve the value of the custom field, save it as a variable and then use it as part of the query_post parameters:

<!-- Modify query object -->
$num_news = get_field("number_news", 523); // number of news items to feature
... rest of the code ...

First: Don't use query_posts

Next you need to add a custom field called 'posts_per_page' to your page.

Finally put this in your function.php while removing the query_posts call from your code.

function limit_home_query( $query ) {
    if ( is_home() && $query->is_main_query() ) {
        // Get the custom field ...
        $posts_per_page = intval(get_field('number_of_latest_news_items_to_promote_on_homepage'));
        // then get '$posts_per_page' posts...
        $query->set( 'posts_per_page', $posts_per_page );
        // ...of type 'news'
        $query->set( 'post_type', array( 'news' ) );
add_action( 'pre_get_posts', 'limit_home_query', 1 );
  • Thanks for your reply but I already have the custom field setup. My problem how do I use the field to control the number of posts in my query (point taken about not using query_posts but can we stick with it for now).
    – Abs
    Feb 20, 2014 at 14:08
  • The code above should do everything you want. Please clarify what doesn't work for you.
    – kraftner
    Feb 20, 2014 at 14:09
  • I won't be able to try the code till tonight but not sure where your code should go in relation to mine (in place of it? after it ? before it?). Also, I have other similar loops in the same template page (hence the rewind_posts), is this a problem?
    – Abs
    Feb 20, 2014 at 14:25
  • Add the other loops to your question as this might be relevant. Also I updated my answer.
    – kraftner
    Feb 20, 2014 at 14:28
  • Thank you, other loop exactly the same but pulling from another post type or a specific category but generally the same in terms of structure (again I won't be able to try code till tonight but I appropriate your help)
    – Abs
    Feb 20, 2014 at 14:31

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.