ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

RSS Feed that limits to date range and orders by view count

June 30, 2014 5:05pm

Subscribe [2]
  • #1 / Jun 30, 2014 5:05pm

    superaven

    107 posts

    Hello,

    I’m trying to build an RSS feed that limits channel entries to a certain date range (Past 7 days ending with previous full day) and then lists them in order of view count (Post popular first, descending) and only shows 6 total.

    I’ve built the RSS feed a while back, though its fairly old so maybe there’s a better way (welcome suggestions), but am a bit hung up on the PHP code for the date. Not sure what I’m doing wrong as an older version of this works.

    I’ve checked my template settings and PHP is set to use and its parsing on input.

    For those wondering, my ultimate goal is using Active Campaign to build a newsletter from a predefined template and using the RSS for the newsletter content and then sending on a schedule (once a week).

    Here’s my code…

    {exp:rss:feed channel="news"}
    <?xml version="1.0" encoding="{encoding}"?>
    <rss versi xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:c>
     <channel>
      <title>{exp:xml_encode}website.com{/exp:xml_encode}</title>
      <link>http://www.website.com/</link>
      <description>website Newsletter RSS Feed</description>
      <dc:language>{channel_language}</dc:language>
      <dc:creator>website.com</dc:creator>
      <dc:rights>Copyright {gmt_date format="%Y"}</dc:rights>
      <dc:date>{gmt_date format="%Y-%m-%dT%H:%i:%s%Q"}</dc:date>
      <admin:generatorAgent rdf:resource="http://expressionengine.com/" >
      <?php $start_time = $this->EE->localize->decode_date('%Y-%m-%d %H:%i', $this->EE->localize->now - 561600); ?>
      {exp:channel:entries channel="news" start_on="<?php echo $start_time; ?>" limit="6" rdf="off" dynamic_start="on" orderby="view_count_one"}
       {exp:nsm_transplant:body}
        {exp:low_replace find=" "replace="SPACE"}
         <item>
          <title>{title}</title>
          <link>http://www.website.com/news/{url_title}</link>
          <guid>{homepage}/news/{url_title}</guid>
          <![CDATA[
           <image>
            {news_thumb}
           </image>
           <thumb>{news_thumb}</thumb>
          ]]>
          <dc:date>{gmt_entry_date format="%Y-%m-%dT%H:%i:%s%Q"}</dc:date>
         </item>
        {/exp:low_replace}
       {/exp:nsm_transplant:body}
      {/exp:channel:entries}
     </channel>
    </rss>
    {/exp:rss:feed}

    Appreciate any assistance.

    Thanks

  • #2 / Jun 30, 2014 6:52pm

    Green Egg Media

    111 posts

    First, depending on the version of EE you are using, you might try switching to the ee() master object instead of the $this->EE format:

    ee()->localize->format_date('%Y-%m-%d %H:%i', ee()->localize->now - 561600);

    Second, you are using the dynamic_start parameter in the Channel Entries tag. This is a special tag for RSS feeds, but it causes the start date to be changed dynamically based on the last time a user requested the feed. If you remove that, do you get the results you are looking for?

  • #3 / Jun 30, 2014 9:02pm

    superaven

    107 posts

    Thanks Green Egg Media for the assistance…

    I’m using EE v2.8.1 - Build Date: 20140314 which I believe is the most current.

    That said, replacing

    $this->EE->localize->decode_date('%Y-%m-%d %H:%i', $this->EE->localize->now - 561600);

    with

    ee()->localize->format_date('%Y-%m-%d %H:%i', ee()->localize->now - 561600);

    fixed the PHP error, so thank you for that.

    I did remove dynamic_start parameter, but doesn’t appear that it affected the results. I did realize that I had forgotten to update the settings in EE so that it would track entry hits, but struggling a bit to figure out if my ordering is correct since I can’t seem to figure out where to view those stats.

    I think I’m close though and certainly appreciate the help you’ve provided with the PHP error.

  • #4 / Jun 30, 2014 9:42pm

    superaven

    107 posts

    Okay, so I found the discrepancy regarding the number of views per entry. For whatever reason, DevDemon’s ‘Hits’ add-on isn’t tracking the same as EE’s entry view counter {view_count_one}. So at least I know I now have them ordered by number of entry views.

    Can someone please help me with the time span offset…

    ee()->localize->format_date('%Y-%m-%d %H:%i', ee()->localize->now - 561600);

    If my goal is to limit the entries to those published in the last 7 days only ending with the last full day (midnight before now), what should that statement look like?

    Trying to wrap my head around PHP dates and times and not getting it.

    Thanks again for the help.

     

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases