0

Apologies... I am more end user than developer so I understand just enough coding to be dangerous! :)

I found a question and answer about redirecting an expired URL to another page, and using that code worked... sort of. Navigating to an expired entry's URL did take me to the index page I wanted it to redirect me to while unexpired entries loaded normally, but my problem is with entries that have no expiration date entered because they are open indefinitely. How do I fix it so that entries with no exp. date set still load while being able to redirect expired ones?

Here's what I have...

{exp:channel:entries channel="{channel_name}" status="open"}

    {if expiration_date < current_time}{redirect="/resources/jobs"}{/if}

    {!-- page content --}

    ...             

{/exp:channel:entries}
2

Well, you have a conditional that must be satisfied in order to achieve your redirect. In order to accommodate your requirements, you must address every condition and you are not (though you may think you are!).

You see, if you don't set an expiration date, that value should output as empty. Think empty... zero... ExpressionEngine is looking at that conditional like this (when there is no {expiration_date} set)..

{if 0 < current_time}

Well, this is always going to be true, so you aren't covering this condition properly. One way to cover this condition is to first make sure that {expiration_date} is set! This is one way to go about it:

{if expiration_date && expiration_date < current_time}

    {!-- Here, we first ask if `expiration_date` is set. If it is,
      -- we move on to check if the expiration date is less than our 
      -- current time. If these two things are true, this gets executed!
      --}

    {redirect="/resources/jobs"}

{/if}
4
  • Shouldn't an expired entry return a no_results true? (not 100% positive on this). In which case you could simply do {if no_results}{redirect="/resources/jobs"}{/if} If you want to have it show expired entries, show_expired="yes", needs to be set. It defaults to "no"
    – Jim Wyse
    Feb 14 '16 at 1:56
  • I think the issue is how to handle channel entries where no expiration_date is set. The conditional the questioner presented handles expired entries and non-expired entries but doesn't handle entries where there is no expiration_date set for that entry.
    – jrothafer
    Feb 14 '16 at 3:42
  • the {if no_results} conditional should cover both cases.
    – Jim Wyse
    Feb 16 '16 at 12:09
  • Thanks for everyone's comments thus far. The template as coded by the developer had {if no_results}{redirect="/resources/jobs"}{/if}, but it didn't work. What happened then when someone went to an expired URL or an incorrect URL, all of the unexpired entries published on one page... each one nested in the previous one. (Example: vachiefs.org/index.php/jobs/item/…) Honestly... the same thing happened even without the {if no_results} code. I'm going to try jrothafer's original suggestion. Feb 17 '16 at 0:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.