1

This my first ever StackOverflow questions so go easy on me!

I have a jQuery UI Datepicker field that I want to use as a basic 'navigation' device to jump straight to a Wordpress Post specific to that date. There are 368 posts over an almost one year period.

I can succesfully use the datepicker to redirect to a url that is based around the date field, e.g:

domain.com/2020-11-07

however, the posts themselves don't use that URL, they have a unique number:

domain.com/365

Is there a way to conditionally change that? I'm currently working around this using .htaccess redirects, but I guess I should avoid that if possible? I'd rather get it working directs from the datepicker script.

One complication, is that the post URLs are not strictly sequential; there are a few duplicates resulting in some like 036, 036B. I'm guessing that rules out doing this in a clever way and means 368 conditional statements. I can create the code easily enough though, once I know what to do. Here's what I currently have for the basic redirect:

$("#datepicker")
.datepicker({
  dateFormat: "yy-mm-dd",
  onSelect: function(dateText) {
    $(this).change();
  }
})

.change(function() {
  window.location.href = "/" + this.value;
});

Any advice gratefully recieved!

Many thanks!

7
  • It's not clear how 2020-11-07 relates to 365. There needs to be some logical relationship.
    – Twisty
    Commented Nov 8, 2020 at 18:09
  • If you can query the list of posts from WP, I assume they have a Unique ID and a post date, you can use onBeforeShow to perform a check and populate data.
    – Twisty
    Commented Nov 8, 2020 at 18:12
  • Thanks for the input. There is no logical relationship between the two unfortunately, it just need to be conditional checked on the date, to return the corresponding URL/slug
    – goto11
    Commented Nov 9, 2020 at 11:50
  • To give a bit more context, each post is a painting. There was a painting created every day for a year. However, as the artist lost track of th number, some were duplicated, hence a 350 AND a 350B. This meant the project ran to 368 days and we also lost any proper sequence. We have a record of exactly what the painting number was for any given day, I just need to conditionally change the Datepicker date to its corresponding painting number. Using PHP I was just do the using something like the following:
    – goto11
    Commented Nov 9, 2020 at 11:52
  • code <?php $this = get_field('date'); if ($this == '2020-10-08') { $slug = '350'; } elseif ($this == '2020-10-09') { $slug = '351'; } elseif ($this == '2020-10-1-') { $slug = '351B'; } ?> <a href="domain.com/<?php echo $slug; ?>">Link</a> code
    – goto11
    Commented Nov 9, 2020 at 11:54

1 Answer 1

0

Consider the following example.

$(function() {
  function getDayOfYear(n) {
    var s = new Date(n.getFullYear(), 0, 0);
    var d = (n - s) + ((s.getTimezoneOffset() - n.getTimezoneOffset()) * 60 * 1000);
    var oneDay = 1000 * 60 * 60 * 24;
    var day = "00" + Math.floor(d / oneDay);
    return day.substr(-3);
  }

  var targetUrl;
  var exceptions = {
    "2020-02-06": "036B",
    "2020-10-10": "283B"
  };

  $("#datepicker")
    .datepicker({
      dateFormat: "yy-mm-dd",
      onSelect: function(dateText) {
        var datePicked = $.datepicker.parseDate("yy-mm-dd", dateText);
        var dayPicked = getDayOfYear(datePicked);
        targetUrl = "/" + dayPicked;
        if (exceptions[dateText] != undefined) {
          targetUrl = "/" + exceptions[dateText];
        }
        $(this).change();
      }
    })
    .change(function() {
      alert("Redirect to " + targetUrl);
      //window.location.href = targetUrl;
    });
});
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<p>Date: <input type="text" id="datepicker"></p>

If the User selects 2020-10-09, they will be redirected /283 as that date is the 283rd day of the Year. If they select 2020-02-02, they will be redirected to /033. The getDayOfYear() will format the result with padding, "001" to "365" and return a String.

You gave an example that the artist had a duplicate on 2020-10-09, two paintings, so if the user selects 2020-10-10, this is an exception. I created an Object exceptions with the Date Text as a Key and the value as the desired special result.

Otherwise I would create an Object with all the Date Text of the year and the corresponding IDs. When a date is picked, you can then just redirect to that value.

var days = {
  "2020-01-01": "/001",
  "2020-01-02": "/002",
  ...
  "2020-02-06": "/036B",
  ...
  "2020-10-10": "/238B",
  ...
  "2020-12-30": "/365",
  "2020-12-31": "/366",
};

Then something like:

window.location.href = days[dateText];

This all depends on the logical correlation between the date selected and the correct URL.

3
  • Woow! Thanks so much for looking into that so thoroughly. I think that should do what I need. One slight extra complication however, is that the project started on Novemeber 18th 2018. How would I add in that starting date offset?
    – goto11
    Commented Nov 12, 2020 at 13:09
  • Also, with the exceptions, would this take into account that additional 'B' date and resume the count from that exception? Thanks once again! @Twisty
    – goto11
    Commented Nov 12, 2020 at 13:11
  • If the project started on 11/18/2018, you'd want to offset the days by that amount. That is the 322nd day of the year in 2018. So 322 : 1, so 01/01/2019 is 1 : 34 and 12/31/2019 is 365 : 393 roughly. Making 11/10/2020 315 : 708. Is that the relationship with some various exceptions?
    – Twisty
    Commented Nov 12, 2020 at 20:48

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.