Support

Account

Home Forums Front-end Issues Advanced Sorting problem

Solving

Advanced Sorting problem

  • Hi..

    I’m writing a magazine/article plugin using ACF.
    I have two custom post types: magazine and article.
    The article contains two custom fields: pagenumber and issue.
    The field issue is a relation field relating to the id of the magazine.
    I also have a custom taxonomy for the articles such as “feature”, “cooking” and so on.

    Now: On a detail page of an issue I’d like to put all the articles below the cover and other details. For selecting them, I’m using my custom field “issue”, for sorting the articles I’d like them to be sorted by the taxonomy and then by pagenumber.

    I’ve tried this:

    $args=array(
    'post_type' => 'article',
    'posts_per_page' => -1,
    'meta_key' => 'issue',
    'meta_value' => $post->ID,
    'orderby' => 'pagenumber',
    'order' => 'ASC'
    );
    $childpages = new WP_Query($args);

    It’s not working since the orderby field seems to not like the field name. I know the example from the documentation, but that doesn’t fit since I need meta_key value for the relationship and can’t declare a different one for sorting.

    I also have no clue how to integrate the custom taxonomy into the query. Any advice is appreciated.

  • You can compare meta values using meta_query in your arguments before starting the loop.

    Check out the WordPress Codex on this topic. https://codex.wordpress.org/Class_Reference/WP_Meta_Query

  • Thanks for your reply. I admit to not understanding your hint.
    I’ve used meta_query as well but it does not support the sorting in combination with the custom field while having a where-clause with a different custom field. Or at least I didn’t get it to work.

    For now I’m building an own array with the data needed and do a multi-sort to get the result I need.

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Advanced Sorting problem’ is closed to new replies.