Suyati Technologies
  • Services
    • Salesforce Services
      • Sales Cloud
      • Service Cloud
      • Marketing Cloud
      • Einstein
      • Experience Cloud
      • Mulesoft
      • Commerce cloud
      • Finance cloud
      • CPQ
      • Consultation
      • Implementation
      • Integration
      • Custom Development
      • Salesforce DevOps
      • Support & Maintenance
      • App Development
      • Managed Services
    • IT Services
      • Content Management Services
      • Analytics
      • RPA
      • Front end Technologies
      • Microsoft Applications
      • Cloud
      • DevOps
      • Snowflake
  • Approach
    • Development Methodology
    • Engagement Model
    • Consulting
  • Intel
    • Blog
    • eBooks
    • Webinars
    • Case Studies
  • About Us
    • Management Team
    • Advisory Board
    • Our Story
    • Testimonials
  • Careers
  • Contact Us
Suyati Technologies
  • Services
    • Salesforce Services
      • Sales Cloud
      • Service Cloud
      • Marketing Cloud
      • Einstein
      • Experience Cloud
      • Mulesoft
      • Commerce cloud
      • Finance cloud
      • CPQ
      • Consultation
      • Implementation
      • Integration
      • Custom Development
      • Salesforce DevOps
      • Support & Maintenance
      • App Development
      • Managed Services
    • IT Services
      • Content Management Services
      • Analytics
      • RPA
      • Front end Technologies
      • Microsoft Applications
      • Cloud
      • DevOps
      • Snowflake
  • Approach
    • Development Methodology
    • Engagement Model
    • Consulting
  • Intel
    • Blog
    • eBooks
    • Webinars
    • Case Studies
  • About Us
    • Management Team
    • Advisory Board
    • Our Story
    • Testimonials
  • Careers
  • Contact Us
Suyati Technologies > Blog > WordPress get_posts method and 5 different usages

WordPress get_posts method and 5 different usages

by Arun Balakrishnan August 2, 2013
by Arun Balakrishnan August 2, 2013 0 comment

The get_posts method:
The very common and appropriate use of this method is to create an array of posts based on given set of parameters. It will fetch a list of recent posts matching this criteria. This method can be used to create Multiple Loops, though a more direct reference.
get_posts method uses WP_Query. get_posts is your best choice in situations where you would like to call an array of posts based on a small set of parameters within a page.
How to Use
Include the below line in your template:
wpss1
Default Arguments
Here we can see the arguments by default:
wpss2
Return Value
It returns an Array – List of post objects
Different usages of get_posts method
1. Posts list with an offset:
If your blog configured to show one post per page, but also want to show links to the previous five posts in category ID 1, you can use this way:
wpss3
Note:This should be used only on a category that has more than one post in it, otherwise there’ll be no output.
2.Fetch all post data:
By default some post-related data is not available to get_posts, such as post content, or the numeric ID. This can be achieved by calling an internal function setup_postdata(), with the $post array as its argument:

wpss4

Without calling setup_postdata(),  you can use $post->COLUMN, where COLUMN is the table column name for the data. So $post->ID holds the ID, $post->post_content the content, and so on. You can use PHPecho command, to print this data.
wpss5
 3. Recent posts ordered by title:
To display the last 10 posts sorted alphabetically in ascending order:
wpss6
the above will display last 10 posts with their post date and title.
4. Random Display:
Display a list of 10 posts selected randomly:
wpss7
This done by using the MySQL RAND() function for the order by parameter value.
5. Show all attachments:
This should be carried out outside of any loops.

wpss8

Here we have just discussed 5 different usages of the get_posts method. We can use this method for many more custom needs For example: fetch posts associated with certain taxonomy, show posts associated with custom field etc.
 

WordpressWordpress get_posts method
0 comment
0
FacebookTwitterLinkedinTumblr
previous post
SQL Bulk copy with trigger in ASP.NET
next post
URL Aliasing in Ektron: What does it mean for your website?

You may also like

What you need to know before kick-starting cloud...

January 13, 2022

An Eye-opener into the Future Trends in Salesforce...

January 13, 2022

Seven Key IT Outsourcing Trends to Expect in...

January 13, 2022

How to Select the Right Partner for a...

January 13, 2022

On Premises vs Cloud CRM: Which is Better?

September 28, 2021

Choosing between Cloud and On-Premise Servers for your...

September 28, 2021

Broken Customer Experience? What’s the Fix?

August 19, 2020

Are Remote Proctored Exams a New Reality?

August 18, 2020

10 Exciting Features in Salesforce’s new Summer ’20...

August 17, 2020

Importance of Data Analytics in Developing Smart Cities

August 11, 2020

Leave a Comment Cancel Reply

Save my name, email, and website in this browser for the next time I comment.

Keep in touch

Twitter Linkedin Facebook Pinterest

Recent Posts

  • What you need to know before kick-starting cloud implementation

    January 13, 2022
  • An Eye-opener into the Future Trends in Salesforce Commerce Cloud

    January 13, 2022
  • Seven Key IT Outsourcing Trends to Expect in 2022

    January 13, 2022

Categories

  • Twitter
  • Linkedin
  • Facebook
  • Instagram
  • Services
    • Salesforce Services
      • Sales Cloud
      • Service Cloud
      • Marketing Cloud
      • Einstein
      • Experience Cloud
      • Mulesoft
      • Commerce cloud
      • Finance cloud
      • CPQ
      • Consultation
      • Implementation
      • Integration
      • Custom Development
      • Salesforce DevOps
      • Support & Maintenance
      • App Development
      • Managed Services
    • IT Services
      • Content Management Services
      • Analytics
      • RPA
      • Front end Technologies
      • Microsoft Applications
      • Cloud
      • DevOps
      • Snowflake
  • Approach
    • Development Methodology
    • Engagement Model
    • Consulting
  • Intel
    • Blog
    • eBooks
    • Webinars
    • Case Studies
  • About Us
    • Management Team
    • Advisory Board
    • Our Story
    • Testimonials
  • Careers
  • Contact Us

© 2021 Suyati Technologies


Back To Top
Suyati Technologies

Popular Posts

  • 1

    What are the Top 3 risks for implementing a CX Program?

    August 30, 2019
  • 2

    Do you need a separate CX Team at your company?

    September 2, 2019
  • 3

    How to build Employee Advocacy for your Business?

    September 3, 2019
  • 4

    What is Salesforce CRM and What Does it Do?

    February 19, 2014
  • 5

    Tips to Reduce Salesforce Pricing

    February 17, 2015
© 2021 Suyati Technologies

Read alsox

Why your investment in the Azure Cloud platform is a...

September 24, 2015

Merge Pdfs Using iTextSharp, C#

December 2, 2010

ZohoCRM to Salesforce: The Whys and Hows of a Seamless...

May 6, 2015

By continuing to use this website you agree with our use of cookies. Read More Agree