We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 51020
    • 670 Posts
    Hi,

    I have a news page, which utilises collections to pull in the news stories.
    This is the getPage code I am using:

    
    [[!getPage?
                          &element=`getResources`
                          &tpl=`newsPost`
                          &parents=`4`
                          &limit=`4`
                          &sortby=`{"publishedon":"DESC"}` 
                        ]]
                        <div class="pagination">[[!+page.nav]]</div>
    
    

    This works fine, but instead of having rolling news stories with paging, I would like to show a complete month instead. So, if I post five resources in December, I want to show all of these on the news home page, rather than limiting to 4 as per the code above.
    At the same token, if there are only 3 stories this month, just show three.

    I have an archive in the sidebar which uses archivist to display the previous months, and they work fine, but I can't seem to use the same principal on the landing page to display the latest month's news.

    Any tips?

    This question has been answered by treigh. See the first response.

      • 36818
      • 119 Posts
      Take a look at this https://forums.modx.com/thread/43773/getresources-where-publishedon.
      Filtering getResources with the where-clause and the Extra getDate.
        • 51020
        • 670 Posts
        Quote from: achterbahn at Jan 18, 2017, 05:07 PM
        Take a look at this https://forums.modx.com/thread/43773/getresources-where-publishedon.
        Filtering getResources with the where-clause and the Extra getDate.

        I haven't come across that - I will take a look!
        Thanks
        Andy
        • discuss.answer
          • 30585
          • 833 Posts
          Here's one way to do it. All due credit to Opengeek. More on the solution here.

          First, create a snippet that returns a unix timestamp. Call it time or whatever you like.
          <?php
          $ts = !empty($input) ? strtotime($input) : time();
          return (string) $ts;
          

          Then use the returned timestamp to compare against the publishedon field of your resource.

          In your getPage call, add a where property:
          &where=`{"publishedon:>":[[time? &input=`-1 month`]]}`
          

          This selects articles published in the current month. If you don't want to limit to 4 articles, set the limit to 0.

          Here's your final call:
          [[!getPage?
            &element=`getResources`
            &tpl=`newsPost`
            &parents=`4`
            &limit=`0`
            &sortby=`{"publishedon":"DESC"}` 
            &where=`{"publishedon:>":[[time? &input=`-1 month`]]}`
          ]]
          <div class="pagination">[[!+page.nav]]</div>
          
            A MODx Fanatic
            • 51020
            • 670 Posts
            Quote from: treigh at Jan 21, 2017, 08:26 AM
            Here's one way to do it. All due credit to Opengeek. More on the solution here.

            First, create a snippet that returns a unix timestamp. Call it time or whatever you like.
            <!--?php
            $ts = !empty($input) ? strtotime($input) : time();
            return (string) $ts;
            

            Then use the returned timestamp to compare against the publishedon field of your resource.

            In your getPage call, add a where property:
            &where=`{"publishedon:-->":[[time? &input=`-1 month`]]}`
            

            This selects articles published in the current month. If you don't want to limit to 4 articles, set the limit to 0.

            Here's your final call:
            [[!getPage?
              &element=`getResources`
              &tpl=`newsPost`
              &parents=`4`
              &limit=`0`
              &sortby=`{"publishedon":"DESC"}` 
              &where=`{"publishedon:>":[[time? &input=`-1 month`]]}`
            ]]
            <div class="pagination">[[!+page.nav]]</div>
            

            That's exactly what I needed - thank you so much for your help!
              • 30585
              • 833 Posts
              Glad it helped!
                A MODx Fanatic