I’ll try to describe what it is I want to achieve, please let me know if too much/not enough detail!

I’m making a site that needs to have documents uploaded to it. These documents will belong to one only of four categories and there will be one page per category. On this page, by default all documents will display. Users will also be able to view only the documents from each year by clicking a link.

It also needs to have pagination so a max of 10 documents are displayed per page.

So how I would ideally like to do this is to make a custom post type (documents) which would then display an ACF custom field set. The field set would have three fields (doc_type, doc_file and release_date). doc_type would be a select field of the four categories, doc_file a file upload and release_date a datepicker field.

When you click on a page, the controller would select by doc_type to only show the documents that belong to that category.

I want to order by release_date and use the year from this field as the display text of my links. For the actual href, I’m imagining something like /path/to/page.php?year=2017.

So there’s a few problems here: first is that I am not sure if what I want to do is even possible – let alone a good way of doing things – with WordPress. I’m more used to pure PHP and mySQL, and so I might be trying to shoehorn more general solutions into WP. Can anyone confirm if it’s possible to achieve what I want?

Second, I’m specifically running into some issues when I try to execute this plan.

I can successfully get each page to only display the documents that belong to it, like so (although I’m generating $last by grabbing info from the URI, which seems dodgy).

$docArgs = array(
    'post_type'     => 'documents',
    'meta_key'      => 'doc_type',
    'meta_value'    => $last,
);

$context['documents'] = Timber::get_posts($docArgs);

When I try to get my sorting on though, de nada:

$docArgs = array(
    'post_type'     => 'documents',
    'meta_key'      => 'doc_type',
    'meta_value'    => $last,
    'meta_query' => array(
        array(
            'key'     => 'release_date',
            'orderby' => 'meta_value_num',
            'order' => DESC,
        ),
    ),
);

I assume I’ve got the syntax wrong, but I’m basically really confused about the whole thing. Any suggestions?

Read more here: Sort and paginate by custom date field with WordPress / ACF / Timber


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: