Support » Plugin: WordPress Infinite Scroll - Ajax Load More » Query only the posts up to date

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Author dcooney

    (@dcooney)

    It looks like you can use the date_query to handle this.
    http://wordpress.stackexchange.com/a/169723

    So, you won’t be able to achieve this with the shortcode builder, you could use the alm_modify_query_args filter to add this query.

    <?php
    function my_custom_alm_modify_query_args( $args, $page ) {
       // $args = current ajax load more $args
       // $page = current page slug
       if($page === 'recent-posts'){
          $args['date_query'] = array(
             array(
                'after' => '1 week ago'
             )
          );
       }
       return $args;
    }
    add_filter( 'alm_modify_query_args', 'my_custom_alm_modify_query_args', 10, 2);

    Thanks! This works!

    However this also changes the behaviour of another AjaxLoadMore shortcode I use on the same page (sidebar).
    Is there any way it would not affect that?

    Plugin Author dcooney

    (@dcooney)

    I don’t think so. When using the filter you can only pass a page slug reference so it will affect all instances on that page.

    If you can’t use the filter, you could run a query before ALM and build an array of post ID’s to pass to the post__in shortcode parameter.
    The same idea as this post__not_in example.
    https://gist.github.com/dcooney/9b037efbd166b4dba5ae

    Let me know if this helps.

    Thanks. This probably helps. I must learn how to build that array of post ID’s that are up to one week old.

    But I’ll just remove the other ALM shortcode from the sidebar for now.

    By the way, as strange as it sounds but the first solution doesn’t work for the front page.
    // $page = current page slug

    It works on any other page though. Any idea how to make it work on the front page?

    So basically it works fine until I choose that page as Front Page from the wordpress Reading Settings.

    What can possibly be the cause of this and is there any solution to this, please?

    Actually never mind. I just learned to implement the other solution:

    <?php
    $recent_alm_posts = new WP_Query( array (
        'date_query' => array(
    		array(
                'after' => '4 week ago'
             )
    		),
    	'posts_per_page' => -1
    ));
    
    $post_ids = wp_list_pluck( $recent_alm_posts->posts, 'ID' );
    $post_ids_string = implode( ',', $post_ids );
    echo do_shortcode('[ajax_load_more post__in="'.$post_ids_string.'"]

    Cheers!

    Plugin Author dcooney

    (@dcooney)

    Nice work!

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Query only the posts up to date’ is closed to new replies.