Home Forums General Issues Reverse query a post relationship on a taxonomy?


Reverse query a post relationship on a taxonomy?

  • Hello,

    first of thank you for the plugin and the support, it is tremendously helpful and worth every cent!

    I have a problem with a relationship field between categories with a custom post type.

    I set up a relationship field for Categories which allows to connect a posts of type product. Now I want to query from the product if it is connected to a category.

    The documentation shows how this is possible using WP_Query and a meta_query, but that just works when both relations are posts, how would that work if one end is a category? I have tried searching but only found this in the WP Codex which is beyond my SQL Knowledge.

    Thanks for any idea!

  • Hi @aplusplus

    May I ask why you have used a relationship field on a taxonomy to relate the taxonomy to a post?

    The point of a taxonomy is that it can be easily related to a post by standard WP functionality. Perhaps you don’t need the relationship field at all?

    That said, to write the code, you first need to explain on what template the code is to run. What is the current WP query on this template and what is the url to access it?

  • Hi Elliot,

    the relationship relates to a custom post type “Product” with a Category.

    Imagine the Category is a Magazine Issue which has Posts (default WP functionality) and then this Magazine Issue Category is connected to a Product to buy the printed Issue. I now want to link to the Category which is connected to the Product from the Product Page. Does this make sense?

    The Code would run on the Template displaying the Products (CPT) where I’d try to query: “Get the Category ID of the Category related to this Product ID”.

    Does this make it clearer?

  • Hi @aplusplus

    I am again a bit confused.

    Can you please explain clearly what fields have been added to which post_type / taxonomy. I need to understand where the data is being saved.

    Also, do you understand what a taxonomy term is normally for? Did you know that you don’t need to use a relationship field to connect a post object to a taxonomy term as this is standard WP behaviour?

  • Sorry if I wasn’t clear, I know about the default WP behaviour and I need something different. This is how it is set up:

    This Relationship field:

    array (
    				'key' => 'field_5231bd6773a84',
    				'label' => 'Product',
    				'name' => 'category_product',
    				'type' => 'relationship',
    				'return_format' => 'object',
    				'post_type' => array (
    					0 => 'product',
    				'taxonomy' => array (
    					0 => 'product_cat:3',
    				'filters' => array (
    					0 => 'search',
    				'result_elements' => array (
    					0 => 'post_type',
    					1 => 'post_title',
    				'max' => 1,

    Is attached to the default Categories:

    'location' => array (
    			array (
    				array (
    					'param' => 'ef_taxonomy',
    					'operator' => '==',
    					'value' => 'category',
    					'order_no' => 0,
    					'group_no' => 0,
    			array (
    				array (
    					'param' => 'ef_taxonomy',
    					'operator' => '==',
    					'value' => 'post_tag',
    					'order_no' => 0,
    					'group_no' => 1,

    My question is how to query the category on the product post type template.
    Hope I was clear and thanks so much for your time!

  • Hi @aplusplus

    Thanks for the info. Now I understand the issue.
    Unfortunately, because of te way that term data is saved (in the wp_options table because term_meta does not yet exist in WP), this kind of query will not be possible at all…

    Perhaps you can remove the relationship field and use another taxonomy to link together products?

    Sorry I can’t be of much help on this one.


  • I see, I’ll the simply need to create the relation from the Post manually, not such a big deal would just be much more elegant to just do it once. Thanks for looking at it, keep up the great work!

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

The topic ‘Reverse query a post relationship on a taxonomy?’ is closed to new replies.