Support

Account

Home Forums General Issues Finding Posts With Relationship And Meta Query

Solving

Finding Posts With Relationship And Meta Query

  • I have a custom post type with a relationship field and the relationship posts have custom fields in them too. I’m trying to create a wp query with the meta key/value found in those relationship posts, but nothing is working so far.

    'post_type' => 'challenges',
    'meta_key' => 'related_services',
    'meta_value' => $id
    );

    The meta key is the relationship field and the $id is the challenges posts with that related service. I can’t seem to find anything in the docs or on the web relating to this kind of thing. I know how to use it in this way if it’s a custom field but related services just bring back an array/post object. So how do I dig into that array?

  • This is a limitation of WP. You can’t do a query that looks for posts based on a meta fields of another post that is realated.

    post => related post => meta fields

    You might want to look at https://codex.wordpress.org/Class_Reference/wpdb

    to do something like this you’d need to construct your own queries.

  • Ah that’s not fun… Looking over that, and being a front-end dev, SQL just blows my mind. I don’t suppose there’s any further light you could shed on how to go about selecting this from the DB? I’m struggling to even get the right posts to show up, let alone ones connected by related posts.

    Cheers.

  • There is also this, which gives examples of building custom queries https://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query

    Unfortunately that also does not show how to do anything with related posts.

    To be honest, while I’ve had a lot of experience in the past with SQL, I’ve never tried to do custom queries like this and have always depended on WP_Query. I’m also finding that it has it’s limitations and have been trying to work out how to use it myself. I have a project coming up that’s going to require exactly what you’re doing.

    It will have a “Product” or “Inventory” post type and a “Specs” post type where each “Spec” can be associated with one or more products and then there will be a parametric search that searches these specs and show the products.

    But I have not found any information specific to this and I’ve been looking for a while. I’d say to ask over on the WP forums, you could get lucky, or maybe stack exchange http://wordpress.stackexchange.com/

    I’m going to be digging into this some more. If I figure anything out I’ll do my best to remember this discussion and post what I find.

  • Okay mate, no worries. I’ll scratch my head some more and if I find anything useful I will post it here too.

    Cheers for the help.

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

You must be logged in to reply to this topic.