Support

Account

Home Forums General Issues How to filter relationship field posts by the posts custom fields

Unread

How to filter relationship field posts by the posts custom fields

  • I’m trying to implement the screen below. It’s a page for a custom post type, called “characters”. The cards on the right are custom post types called “spells”, which are being queried from the relationship field on the character post.

    The page I'm trying to implement

    I have managed to implement the spell cards on the right without any problem, using this loop:

    $knownSpells = get_field('char_spells');
    foreach($knownSpells as $spells) {
    //The template goes here
    }

    However, for the sidebar, I need to group these spells by a custom field called “levels”, and I don’t know how to do that. If the get_field('related_posts') returned information about custom fields, I could use a array_filter() funtion, but from what I’ve seen, that’s not possible (I would love to be proven wrong, though).

    I have seen 2 posts linking to this function, but I don’t really understand how I’m supposed to use it, or if it’s even supposed to help in my situation. I did a little test with the code below, to see if it would change anything in my query, and it did nothing. I don’t know if I should call that specific query on the page or what.

    Something that I feel may solve my issue is this plugin, which shows the custom fields in the rest api, but I’m not really sure how to interact with json files yet, and it sends back a fuckton of data, which I feel may not be very optimal, but if I could do something similar with the regular wordpress query, I would be well on my way to solve this issue.

Viewing 1 post (of 1 total)

The topic ‘How to filter relationship field posts by the posts custom fields’ is closed to new replies.