• Skip to main content

Victor Font Consulting Group, LLC

Strategic IT Solutions for Complex Organizations

  • Home
  • Care Plans
    • Care Articles
    • Optional Subscriptions
  • Consultations
  • Products
    • Code Snippets
    • Public GitHub Repositories
    • Gist Snippets
    • Pastebin Snippets (Free)
    • Free Plugins
  • FAQs
  • Support
    • Graphic Design
  • Contact
    • Speakers
    • Portfolio
  • Resources
    • Free WordPress Video Training
    • Tutorials
    • Articles
    • Cybersecurity
    • EU Referral Network

Limit Number of Posts Returned in WP Query

posted on September 25, 2015

How to Limit Number of Posts

Did you know that you can place a limit on the number of posts in a WordPress Query? WordPress has a built in filter that you can use to take advantage of the MySQL SELECT statement LIMIT clause. In SQL, a LIMIT clause constrains the number of rows returned by the SELECT statement. Be careful when you use though, because on some server environments the LIMIT is applied to all queries on the page. This means menu items and widgets may also be limited to the defined number.

LIMIT takes one or two numeric arguments. With two arguments, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return. The offset of the first row is 0, not 1.

Each of the code snippets below limits query results set to 40 records. If you want to change that number, change the '40' in line 4 to your preference.

Add this code to functions.php to limit the number of posts returned in all queries:

Add this code to functions.php to limit the number of posts returned in search queries:

  • 1share
  • Facebook0
  • Twitter0
  • Pinterest0
  • LinkedIn1
  • Print
  • SMS0

About Victor M. Font Jr.

Victor M. Font Jr. is an award winning author, entrepreneur, and Senior IT Executive. A Founding Board Member of the North Carolina Executive Roundtable, he has served on the Board of Advisors, of the North Carolina Technology Association, the International Institute of Business Analysis, Association of Information Technology Professionals, Toastmasters International, and the North Carolina Commission for Mental Health, Developmental Disabilities, and Substance Abuse Services. He is author of several books including The Ultimate Guide to the SDLC and Winning With WordPress Basics, and Cybersecurity.

 
We only use analytical cookies on our website that allow us to recognize and count the number of visitors, but they do not identify you individually. They help us to improve the way our website works. By clicking Accept you, agree to cookies being used in accordance with our Cookie Policy.