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.

Filter results based on date in variable

September 24, 2011 6:43pm

Subscribe [3]
  • #1 / Sep 24, 2011 6:43pm

    clkent

    4 posts

    I’ve been working on adding some things to a client’s EE 1.6.8 site and one of the requests was to add a sort by year drop-down to one of the article pages.

    here:
    http://www.earth-policy.org/press_room/in-the-news/web

    The page is currently being sorted by setting the uri segment_3 to either audio, video etc. and then using the populated segment_3 and the variable media_type set for each entry to display the correct entries below.

    I set up the year drop-down the same way, adding the year to segment_4 and keeping segment_3 and then I set the year parameter in the exp:weblog:entries tag bellow to {segment_4}.  This all works fine except it is doing the sorting based on the entries date and apparently I need to sort based on a created variable date, “publication_date”.

    So the real issue here is I need to figure out how to dynamically make the content filter based on the publication date year and not based on the actual entry date.

    Here’s the code below that works for sorting by entry date just to get an idea of how it’s structured or set up… (also I’ve just been hired to make this part work, I didn’t build the site out this way to begin with and I don’t know if I can really change a lot)

    Dropdown:
    <form name=“typemenu”>
      <select name=“seltype”>
      <option value=”“>—Select Media Type—</option>
      <option value=”/press_room/C69”>—Show All Types—</option>
          <option value=”/press_room/in-the-news/print”>Print</option>
          <option value=”/press_room/in-the-news/audio”>Audio</option>
          <option value=”/press_room/in-the-news/video”>Video</option>
          <option value=”/press_room/in-the-news/web”>Web</option>
      </select>

    <select name=“orderby”>
        <option value=”“>—Select Year—</option>
      <option value=”/press_room/in-the-news/{segment_3}”>—Show All Years—</option>
      <option value=”/press_room/in-the-news/{segment_3}/2011”>2011</option>
      <option value=”/press_room/in-the-news/{segment_3}/2010”>2010</option>
      <option value=”/press_room/in-the-news/{segment_3}/2009”>2009</option>
      <option value=”/press_room/in-the-news/{segment_3}/2008”>2008</option>
      <option value=”/press_room/in-the-news/{segment_3}/2007”>2007</option>
      <option value=”/press_room/in-the-news/{segment_3}/2006”>2006</option>
      <option value=”/press_room/in-the-news/{segment_3}/2005”>2005</option>
      <option value=”/press_room/in-the-news/{segment_3}/2004”>2004</option>
      <option value=”/press_room/in-the-news/{segment_3}/2003”>2003</option>
      <option value=”/press_room/in-the-news/{segment_3}/2002”>2002</option>
      <option value=”/press_room/in-the-news/{segment_3}/2001”>2001</option>
      <option value=”/press_room/in-the-news/{segment_3}/2000”>2000</option>
      </select>
    </form>

    Inside the select tags there is also an onchange with “location=document.typemenu.orderby.options[document.typemenu.orderby.selectedIndex].value;”
    for both.

    Opening of weblog tag:
    {exp:weblog:entries weblog="epi_in_the_news" category_group="not 999" orderby="publication_date" sort="desc" limit="1000" year="{segment_4}”}

  • #2 / Sep 26, 2011 10:11am

    Mark Bowen

    12637 posts

    Hi clkent,

    This is more of a how-to kind of post and not technically a support question.

    I’m going to move it on down to the Community Help forums for you so that others can get a look in on this and might be able to lend a hand with this.

    That said do you have the year drop-down working anywhere on a page yet? When I went to the link above I could only see the one that filters by type?

    Mark

  • #3 / Sep 26, 2011 8:09pm

    clkent

    4 posts

    Sorry about posting this in the wrong place.

    I don’t have it live anywhere because it is filtering based on entry date year and I need it to filter based on “publication_date”  which is a custom date field.

    Any ideas how to get it to sort based off of the year from a custom date field?

  • #4 / Sep 26, 2011 10:01pm

    John St-Amand

    865 posts

    Is it just that when a person selects “2006” you want to only show that year from the publication_date custom field?  Is the year could be isolated in the custom field you could add a search parameter to the entries loop that’s reads the segment_4, like:

    {exp:channel:entries weblog=“epi_in_the_news” category_group=“not 999” orderby=“publication_date” sort=“desc” limit=“1000” search:publication_date=”{segment_4}”}

    But if it’s a date field with day and month, I doubt that will work without the format parameter on the date itself.

    You could just put a conditional inside the entries loop, but that’s not a very efficient way to do it, though it would certainly work.  So you would use something like:

    {if '{publication_date format="%Y"}' == '{segment_4}'}
    your content for that year
    {/if}

    You could wrap that in a simple conditional to only apply it IF segment_4 is present, assuming you have an unfiltered view as well using the same template.

    I would suspect the more efficient way to go would be to use a query to search the custom field in the channel for the year in the segment and only pull out the right ones.  With the code above it would really loop through every entry regardless of year but only display the year selected - which might be a bit too memory intensive for you.

    Hope that helps.  I’m sure someone knowledgable with queries will chime in with a better solution.

  • #5 / Sep 27, 2011 11:28pm

    clkent

    4 posts

    John, thanks for offering up a solution. I’m not sure if I can get it to work that way unfortunately because the publication_date custom field is set as a Date Field and I’m pretty sure you can only use the search parameter with Text Input, Textarea, or Drop-down Lists.

    I’m sure there is some way to write a query like you were saying, unfortunately that is a bit over my head.

  • #6 / Sep 30, 2011 12:28pm

    clkent

    4 posts

    Still stuck on this. Anyone know how to make this happen with writing a query?

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

ExpressionEngine News!

#eecms, #events, #releases