• Resolved charafweb

    (@charafweb)


    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

    (@ahmedkaludi)

    okay, where exactly do you want the loop output to be displayed?

    Thread Starter charafweb

    (@charafweb)

    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

    (@ahmedkaludi)

    When I said “Where”, I mean where in the frontend.

    You can use the amp_loop for it: https://ampforwp.com/tutorials/article/loop/

    Thread Starter charafweb

    (@charafweb)

    Hi @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/), 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

    (@ahmedkaludi)

    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

    (@charafweb)

    Hi @ahmedkaludi ,

    In homepage, between the header and footer, as main content, inside content-wrapper div 🙂

    Thank you

    Plugin Author Ahmed Kaludi

    (@ahmedkaludi)

    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

    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

    (@charafweb)

    Thank you very much. Yes, that code did it 🙂

    Plugin Author Ahmed Kaludi

    (@ahmedkaludi)

    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.