0

I have a page on a site that is showing entries with this code:

{% extends '_layouts/default.twig' %}

{% macro productItem(product) %}
    {% set filterClasses = [] %}
    {% for material in product.productMaterials %}
        {% set filterClasses = filterClasses|merge(['material-' ~ material.slug]) %}
    {% endfor %}

    {% if product.productIsModular %}
        {% set filterClasses = filterClasses|merge(['product-modular']) %}
    {% endif %}

    <div class="cell filtered-product {{ filterClasses|join(' ') }}">
        <div class="card card--productlist small">
            <section class="card-heading">
                <a href="{{ product.url }}"
                   class="head product-name">{{ product.title|upper }}</a></span>
            </section>
            <section class="card-image card-image--slide">
                <div class="card-slide slick-dots-2">
                    {% for thumb in product.productThumbnail %}
                        <div>
                            <figure>
                                <a href="{{ product.url }}"><img
                                        data-src="{{ thumb.url('productListingThumbnail') }}"
                                        data-srcset="{{ thumb.url('productListingThumbnail') }} 1x, {{ thumb.url('productListingThumbnail2x') }} 2x"></a>
                            </figure>
                        </div>
                    {% endfor %}
                </div>
            </section>
        </div>
    </div>
{% endmacro %}
{% import _self as self %}

{% block content %}
        {% set products = craft.entries.section('products')
            .siteId(entry.siteId)
            .productIsNew('not 0')
            .with([
            ['productThumbnail', { withTransforms:
                [
                    'productListingThumbnail',
                    'productListingThumbnail2x'
                ]}
            ],
            'productMaterials'
        ]).all() %}

        {% set materials = craft.entries.section('materials').relatedTo({
            sourceElement: products,
            field: 'productMaterials'
        }).all() %}
            <div class="page-product-listing page-inner-padding">
                <div class="page-inner-margin">

                    <!-- Page inner heading -->
                    <div id="page-inner-heading">
                        <div class="grid-container">
                            <div class="grid-x grid-margin-x align-middle">
                                <div class="cell medium-6">

                                    <!-- Breadcrumbs -->
                                    <nav aria-label="{{ 'You are here' | t }}:"
                                         role="navigation">
                                        <ul class="breadcrumbs">
                                            <li>
                                                <a href="/products">{{ 'products'|t }}</a>
                                            </li>
                                            <li>
                                        <span
                                            class="show-for-sr">{{ 'Current'|t }}
                                            : </span> {{ entry.title | lower }}
                                            </li>
                                        </ul>
                                    </nav>

                                    <!-- Page heading title -->
                                    <h1 class="h2">{{ entry.title }}</h1>
                                </div>
                                <div class="cell medium-6 small-12">
                                    <div id="filter-fields">
                                        <form>
                                            <div class="rt filter">
                                                <div class="form-label">{{ 'Filter
                                            results' | t }}:
                                                </div>
                                                <div class="custom-select">
                                                    <select
                                                        id="select-material">
                                                        <option
                                                            value="">{{ 'all materials' | t }}</option>
                                                        {% for material in materials %}
                                                            <option
                                                                value="material-{{ material.slug }}">{{ material.title }}</option>
                                                        {% endfor %}
                                                    </select>
                                                </div>

                                            </div>
                                            <div class="filter filter--option">

                                                <div
                                                    class="form-label radiocheck">
                                                    <!-- Custom Radio -->
                                                    <div
                                                        class="custom-input checkbox">
                                                        <input type="checkbox"
                                                               id="check-modular"/>
                                                        <div></div>
                                                    </div>
                                                    <label
                                                        for="check-modular">{{ 'only modular' | t }}</label>
                                                </div>

                                            </div>
                                        </form>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div id="product-listing-slide">
                        <div class="grid-container">
                            <div
                                class="grid-x grid-padding-x small-up-2 medium-up-3 large-up-5"
                                id="filtered-products">
                                {% for product in products %}
                                    {{ self.productItem(product) }}
                                {% endfor %}
                            </div>

                        </div>
                    </div>

                </div>
            </div>
{% endblock %}

This renders the new products on the page and they are displaying sorted by title alphabetically. I'd like them to be sorted by newest entries first.

I'm still learning about Craft but from what I understand, I should be able to filter the order of this entries section by adding this code:

.orderby('postDate DESC')

I added that here:

        {% set products = craft.entries.section('products')
            .siteId(entry.siteId)
            .productIsNew('not 0')
            .orderby('postDate DESC')

But no change is made.

I've tried using

  • .order('postDate DESC')
  • .orderby('postDate DESC')
  • .order('dateCreated DESC')
  • .orderby('dateCreated DESC')
  • .addOrderBy('dateCreated DESC')
  • .orderBy('entries.dateCreated DESC')

But there is no order change. I'm not sure why I can't get the order to work.

I believe I'm in the right spot though because when I enter .title('*line*') I only get the products that have "line in their title"

My end goal is to have this page sort by date entered and but only show the product posts that were created within the last year.

I'm on craft 3.9.6 Pro.

0

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.