Title: Custom loop
Last modified: July 6, 2020

---

# Custom loop

 *  Resolved [charafweb](https://wordpress.org/support/users/charafweb/)
 * (@charafweb)
 * [5 years, 11 months ago](https://wordpress.org/support/topic/custom-loop-9/)
 * Hi,
 * I have this filter on my theme functions.php . As you can see, this loops through
   posts and displays only posts that have a custom field called calendar_date filled
   with a date >= today date. So that, we **display only upcoming events**.
 * **How can we do the same thing with Swift theme ?**
 *     ```
       function my_events_ie($query) {
   
         if (!is_admin() && is_singular() && $query->is_main_query()) {
   
           $query->set('posts_per_page', -1);
           $query->set('no_found_rows', true);
           $query->set( 'meta_key', 'calendar_date' );
           $query->set( 'order', 'ASC');
           $query->set( 'meta_query', array(
               array(
                   'key'     => 'calendar_date',
                   'compare' => '>=',
                   'value'   => current_time('Y-m-d'),
                   'type'    => 'DATE',
               )
           ) );
        }
   
         //return $query;
   
       }
   
       add_filter('pre_get_posts', 'my_events_ie');
       ```
   

Viewing 9 replies - 1 through 9 (of 9 total)

 *  Plugin Author [Ahmed Kaludi](https://wordpress.org/support/users/ahmedkaludi/)
 * (@ahmedkaludi)
 * [5 years, 11 months ago](https://wordpress.org/support/topic/custom-loop-9/#post-13086428)
 * okay, where exactly do you want the loop output to be displayed?
 *  Thread Starter [charafweb](https://wordpress.org/support/users/charafweb/)
 * (@charafweb)
 * [5 years, 11 months ago](https://wordpress.org/support/topic/custom-loop-9/#post-13089076)
 * I want the loop output to be displayed into homepage.
 * I did a mistake here :
 * `if (!is_admin() && is_singular() && $query->is_main_query())`
 * I think it should be :
 * `if (!is_admin() && is_home() && $query->is_main_query())`
 * Thanks you very much
 *  Plugin Author [Ahmed Kaludi](https://wordpress.org/support/users/ahmedkaludi/)
 * (@ahmedkaludi)
 * [5 years, 11 months ago](https://wordpress.org/support/topic/custom-loop-9/#post-13093842)
 * When I said “Where”, I mean where in the frontend.
 * You can use the amp_loop for it: [https://ampforwp.com/tutorials/article/loop/](https://ampforwp.com/tutorials/article/loop/)
 *  Thread Starter [charafweb](https://wordpress.org/support/users/charafweb/)
 * (@charafweb)
 * [5 years, 11 months ago](https://wordpress.org/support/topic/custom-loop-9/#post-13095057)
 * Hi [@ahmedkaludi](https://wordpress.org/support/users/ahmedkaludi/)
 * I know you are so busy, so thank you for giving me some of your time to help 
   on this.
 * I read the tuto ([https://ampforwp.com/tutorials/article/loop/](https://ampforwp.com/tutorials/article/loop/)),
   but I can’t figure what & how to do it ! How to pass custom $args to the loop
   for instance ? Or how to set new values ($query->set) ?
 * Would you please elaborate on how and what to add to the functions.php ?
 * Is it possible to make a full example, so it can be as reference to other people
   looking to achieve the same thing ? Let’s say we want to create a widget that
   will display only upcoming events based on a date stored into a meta field. How
   to achieve this? I did google about this but I can’t found anything.
 * Thank you again
 *  Plugin Author [Ahmed Kaludi](https://wordpress.org/support/users/ahmedkaludi/)
 * (@ahmedkaludi)
 * [5 years, 11 months ago](https://wordpress.org/support/topic/custom-loop-9/#post-13099321)
 * I want to give you solution, but before I do that, I have one doubt which needs
   to be cleared.
 * Where exactly do you want the loop output to be displayed?, “When I said “Where”,
   I mean where in the frontend.
 *  Thread Starter [charafweb](https://wordpress.org/support/users/charafweb/)
 * (@charafweb)
 * [5 years, 11 months ago](https://wordpress.org/support/topic/custom-loop-9/#post-13099727)
 * Hi [@ahmedkaludi](https://wordpress.org/support/users/ahmedkaludi/) ,
 * In homepage, between the header and footer, as main content, inside content-wrapper
   div 🙂
 * Thank you
 *  Plugin Author [Ahmed Kaludi](https://wordpress.org/support/users/ahmedkaludi/)
 * (@ahmedkaludi)
 * [5 years, 11 months ago](https://wordpress.org/support/topic/custom-loop-9/#post-13105503)
 * Thanks for the detailed information. We have a filter ampforwp_query_args. From
   which you can modify the loop query on any pages.
    And that is available in accelerated-
   mobile-pages/components/loop/loop.php line no: 222 [https://github.com/ahmedkaludi/accelerated-mobile-pages/blob/master/components/loop/loop.php#L222](https://github.com/ahmedkaludi/accelerated-mobile-pages/blob/master/components/loop/loop.php#L222)
 * I think you can use this code to achieve the result you want:
 * add_filter(‘ampforwp_query_args’, ‘custom_function’);
    function custom_function(
   $q){
 *  if ( function_exists(‘ampforwp_is_home’) && ampforwp_is_home() ) {
    $q[‘meta_query’]
   = array( array( ‘key’ => ‘calendar_date’, ‘compare’ => ‘>=’, ‘value’ => current_time(‘
   Y-m-d’), ‘type’ => ‘DATE’, ), ); }
 *  return $q;
    }
 *  Thread Starter [charafweb](https://wordpress.org/support/users/charafweb/)
 * (@charafweb)
 * [5 years, 11 months ago](https://wordpress.org/support/topic/custom-loop-9/#post-13106736)
 * Thank you very much. Yes, that code did it 🙂
 *  Plugin Author [Ahmed Kaludi](https://wordpress.org/support/users/ahmedkaludi/)
 * (@ahmedkaludi)
 * [5 years, 11 months ago](https://wordpress.org/support/topic/custom-loop-9/#post-13107931)
 * We are glad that your issue has been resolved.

Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘Custom loop’ is closed to new replies.

 * ![](https://ps.w.org/accelerated-mobile-pages/assets/icon-256x256.png?rev=1693616)
 * [AMP for WP - Accelerated Mobile Pages](https://wordpress.org/plugins/accelerated-mobile-pages/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/accelerated-mobile-pages/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/accelerated-mobile-pages/)
 * [Active Topics](https://wordpress.org/support/plugin/accelerated-mobile-pages/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/accelerated-mobile-pages/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/accelerated-mobile-pages/reviews/)

 * 9 replies
 * 2 participants
 * Last reply from: [Ahmed Kaludi](https://wordpress.org/support/users/ahmedkaludi/)
 * Last activity: [5 years, 11 months ago](https://wordpress.org/support/topic/custom-loop-9/#post-13107931)
 * Status: resolved