Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Eliot Akira

    (@miyarakira)

    Thanks for the suggestion – that’s an interesting idea, to group by field value.

    I’m thinking how it could be implemented. It sounds similar to sorting, for example:

    [loop type="events" orderby="field" field="start_date"]
      Event: [field title] from [field start_date] to [field end_date]
    [/loop]

    This should work if the field value is Ymd or Y-m-d format, I guess timestamp too. The question is how to group them by month.. I’m not sure how groups could work inside the loop. It seems like each group is a separate loop. Hmm..

    How about using a list..

    [pass list="2015-01, 2015-02, 2015-03, ..."]
      Month: {ITEM}
      [loop type="events" orderby="field" field="start_date" start="{ITEM}"]
        Event: [field title] from..
      [/loop]
    [/pass]

    Assuming the field is stored as Y-m-d, the above would go through each month in the list, and builds a loop with events whose start date begins with that year and month.

    It’s a bit far-fetched – I wonder how it could be “automated” and simpler to write. The difficulty I see is where it displays the “group title”, the heading for each month. It looks like that would need to be outside the loop.

    Could you give me an example of what you imagine the resulting code might look like?

    Plugin Author Eliot Akira

    (@miyarakira)

    Here’s a little improved version:

    [pass list="2015-01, 2015-02, 2015-03, ..."]
      [loop type="events" orderby="field" field="start_date" start="{ITEM}"]
        [if first]Month: {ITEM}[/if]
        Event: [field title] from..
      [/loop]
    [/pass]

    That should skip any month that doesn’t have a matching post. So, I hope we’re getting closer. I suppose then the question is how to automatically generate the list of months.

    Thread Starter MarkPGray

    (@markpgray)

    Hi, man you are quick!

    Personally I am out of my depth but I found these posts that may be of use…

    https://wordpress.stackexchange.com/questions/151469/group-wp-query-by-meta-key-date

    https://wordpress.stackexchange.com/questions/25381/group-posts-by-custom-meta-value-date

    This one was grouping alphabetically and gave a pastebin link for code that worked.

    https://www.ads-software.com/support/topic/group-posts-by-custom-field-alphabetically?replies=15

    with code that worked here https://pastebin.com/b4WGrMhD

    Thread Starter MarkPGray

    (@markpgray)

    Here’s another idea, the solution looked really elegant to me but may not translate to wordpress as it is twig, but…

    {% set date = null %}
        {% for event in events %}
            {% if date != event.startdate %}
                {% set date = event.startdate %}
                <h3>{{ date }}</h3>
            {% endif %}
    
            <p>{{ event.name }}</p>
        {% endfor %}

    here’s the link…

    https://stackoverflow.com/questions/23896701/twig-foreach-group-by-date

    Plugin Author Eliot Akira

    (@miyarakira)

    I see.. The above example in twig is similar in concept to the other links – basically to output a header for the first occurrence of each date. That would work for grouping by individual days, but not months.

    This is a tough one. I think the field values (start date) need to be parsed, to extract the months – then, to display a heading for first occurrence of each month.

    Here’s how I imagine it working like:

    [loop type="events" orderby="field" field="start_date" group="month"]
      [if month] {YEAR} / {MONTH} [/if]
      Event: [field title] from [field start_date]
    [/loop]

    The parameter group could be set to day, month, year or – say, value, which would group by the whole field value..

    One issue could be, that the heading can only display the month number. I suppose there could be a conditional like:

    [if month="1"] {YEAR} January [/if]

    ..but you’d have to code all 12 months.

    So, it will not be so simple to do.

    Interestingly, there was another recent request of similar complexity – sorting by multiple custom fields. WP_Query doesn’t cover it – same with grouping by fields – and it’s tough to come up with a good solution.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Group by custom field’ is closed to new replies.