Title: Query for recent FAQs using query_posts or get_posts
Last modified: August 24, 2016

---

# Query for recent FAQs using query_posts or get_posts

 *  Resolved [TrishaM](https://wordpress.org/support/users/trisham/)
 * (@trisham)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/query-for-recent-faqs-using-query_posts-or-get_posts/)
 * Thank you for this brilliant plugin – it does exactly what I need it to do and
   it does it beautifully. 🙂
 * I could use some help in constructing a query to show the most recent X number
   of FAQs in a sidebar widget, my code is not working.
 * Rather than hardcoding in the slug, I need to find it using the query – when 
   looking at my FAQs page source code, I see that the ID of the FAQ is the Post-
   >ID, so I’m using this:
 *     ```
       <ul>
       <?php
         query_posts( array( 'post_type' => 'faq', 'posts_per_page' => 5 ) );
         if ( have_posts() ) : while ( have_posts() ) : the_post();
       ?>
         <li><a href="<?php get_site_url(); ?>/faqs/#faq-<?php echo $post->ID ?>"><?php the_title(); ?></a></li>
       <?php endwhile; endif; wp_reset_query(); ?>
       </ul>
       ```
   
 * This works great to produce a short list for my sidebar of the 5 most recent 
   FAQs, but the linking isn’t right – they all go to a link that is this:
 * [http://myrealdomain.com/faqs/#faq-](http://myrealdomain.com/faqs/#faq-)
 * That’s it – no post ID…..so each link takes you to the FAQ page, but not directly
   to the linked FAQ.
 * I’m sure I’m missing something very simple, but I cannot figure it out…..any 
   suggestions please?
 * [https://wordpress.org/plugins/arconix-faq/](https://wordpress.org/plugins/arconix-faq/)

Viewing 1 replies (of 1 total)

 *  Thread Starter [TrishaM](https://wordpress.org/support/users/trisham/)
 * (@trisham)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/query-for-recent-faqs-using-query_posts-or-get_posts/#post-5965075)
 * Doh! I figured it out.
 * Don’t you hate when you struggle with something, then finally give in and post
   in a forum for help, and THEN figure it out only moments later?
    Yeah, me too.
 * OK so for the benefit of anyone else wanting to do this, the only change I made
   to my code above is to replace $post->ID with the_ID();
 * SO the code to query the custom post type “faq” and produce a list of links to
   those FAQs is this:
 *     ```
       <ul>
       <?php
         query_posts( array( 'post_type' => 'faq', 'posts_per_page' => 5 ) );
         if ( have_posts() ) : while ( have_posts() ) : the_post();
       ?>
         <li><a href="<?php get_site_url(); ?>/faqs/#faq-<?php the_ID(); ?>"><?php the_title(); ?></a></li>
       <?php endwhile; endif; wp_reset_query(); ?>
       </ul>
       ```
   
 * Adjust the ‘posts_per_page’ to the number you want to display, and of course 
   you can hard-code the site url instead of making the PHP call to get it, I am
   doing it only during the development phase and will hard-code the URL when it’s
   ready to go live.

Viewing 1 replies (of 1 total)

The topic ‘Query for recent FAQs using query_posts or get_posts’ is closed to new
replies.

 * ![](https://ps.w.org/arconix-faq/assets/icon-256x256.jpg?rev=3050529)
 * [Arconix FAQ](https://wordpress.org/plugins/arconix-faq/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/arconix-faq/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/arconix-faq/)
 * [Active Topics](https://wordpress.org/support/plugin/arconix-faq/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/arconix-faq/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/arconix-faq/reviews/)

 * 1 reply
 * 1 participant
 * Last reply from: [TrishaM](https://wordpress.org/support/users/trisham/)
 * Last activity: [11 years, 2 months ago](https://wordpress.org/support/topic/query-for-recent-faqs-using-query_posts-or-get_posts/#post-5965075)
 * Status: resolved