Help Center
Macros for WordPress Relations

Macros for WordPress Relations

This tutorial describes how to display the related parent, children, sibling, grandparent, and grandchildren posts, terms, users, and CCT items with the help of the WordPress JetEngine plugin.

Warning

Be aware that the legacy macros work only with the legacy relations. The refactoring macros described in this tutorial work with the refactoring relation that you can create after the 2.11.0 JetEngine update.

legacy and refactoring macros

If you created a parent-child relationship and want to show related items – this macro is for you.

Things to know

This macro displays parent or children items of the object.

This macro allows you to get children or parent posts related to the object. For example, if you have the Providers and Services related post types, you can display only those services that the particular provider offers.

related parent post and children posts

Navigate to JetEngine > Query Builder, and hit the “Add New” button.

Give your query a Name. Select the type of parent or children items you want to display in the Query Type field. For example, if you need to get the child posts of the parent user, choose the “Post Query” option.

Posts Query

If you need to get the related items from the post type, select the “Post Query” option in the Query Type field. In the General section, specify the Post Type.

posts query type for the related items

Open the Post & Page section, click on the macros icon near the Post In field and select the “Related Items” option.

macros in query

There are such settings in the Related Items window:

  • From Relation. Specify the relation from where you want to get parent or children items;
  • From Object. Specify if you want to display children (Child Object option) or parent (Parent Object option) items;
  • Initial Object ID From. Indicate where the query will get the object ID from to display its related items:
    • Current Object ID. Shows the related items of the current post on the Single Page (as in our example, the query shows the related services of the current provider post), of the current active listing item, and so on;
    • Current User ID. Shows the related items of the currently logged-in user;
    • Queried User ID. Shows the related items of the selected user, e.g., on the User Page in the Profile Builder;
    • Query Variable. Shows the related items from the global request or WordPress request variables. If you select this option, the fourth Variable Name will appear;
    • Current Object Variable. Shows the related items from the object defined as current by JetEngine. If you select this option, the fourth Variable Name will appear;
    • Default WordPress Object (for current page). Always shows the related items of a current global object for the current page defined by WordPress, as opposed to the “From Relation” option. For instance, on a single page/single post, it will always be the current post, even if you go to the listing added to this page.
  • Variable Name. This field appears if you select the “Query Variable” or “Current Object Variable” options. This option allows setting the variable’s name, which the plugin will search for in the query or current object.
related items macros

We have set such a query for the relation in this tutorial. After you finish, click on the “Apply” and “Add Query” buttons.

query for related post types

Create a Listing Item for the related children/parent post you want to display if you haven’t done it yet. Then fill it with content using, for example, Dynamic Field or other dynamic widgets. Don’t forget to publish it.

setup listing item for posts

Proceed to the Single Page (or the Archive Page for the related terms) with parent/child item (e.g., post) and place the Listing Grid with the children/parent items there. Open the Custom Query section in the Listing Grid widget, enable the Use Custom Query toggle, and select the name of the needed Custom Query. After you finish editing, hit the “Publish” or “Update” button.

listing grid custom query

Terms Query

Select the “Terms Query” option in the Query Type field to get the related items from the taxonomy. In the General section, choose the Taxonomy name or several names.

terms query type

Open the Include/Exclude section, hit the macros icon near the Include field, and select the “Related Items” option. Then, set the macro options according to your needs. We have described all macro settings in the Posts Query paragraph.

related items macro for terms

After you finish, click on the “Apply” and “Add Query” buttons.

Create a Listing Item for the related children/parent terms you want to display if you haven’t done it yet.

setup listing item for terms

Fill it with content with the help of, for example, Dynamic Field or other dynamic widgets.

listing item for terms with the dynamic field

Proceed to the Single Page (or the Archive Page for the related terms) with parent/child item (e.g., post) and place the Listing Grid with the children/parent items there. Open the Custom Query section in the Listing Grid widget, enable the Use Custom Query toggle and select the name of the needed Custom Query. After you finish editing, hit the “Publish” or “Update” button. Then, you can navigate to the front end and look at the result.

related terms on the single page

Users Query

Select the “Users Query” Type if you decide to display related users. Proceed to the Include/Exclude section, hit the macros icon near the Include field, and select the “Related Items” option. Then, set the macro options according to your needs. After you finish, click on the “Apply” and “Add Query” buttons.

users query type for relations

Then, create a Listing Item for users and drag-and-drop the Listing Grid widget on the Single Page (or you can place it on the Archive Page for the related terms). Open the Custom Query section in the Listing Grid widget, enable the Use Custom Query toggle, and select the name of the needed Custom Query. After you finish editing, hit the “Publish” or “Update” button. Then, you can navigate to the front end and look at the result.

related users on the front end

Custom Content Type Query

If you decide to display related Custom Content Type items, select the “Custom Content Type Query” Type and select the name of the needed CCT in the From Content Type field.

custom content type query for relations

Hit the “Add new” button in the Query section. In the repeater field, pick the “Item ID” option for the Field and the “In the list” for the Compare. Hit the “coin stack” icon near the Value field, and select the “Related Items” option. Then, set the macro options according to your needs. At last, select the “Numeric” Type. After you finish, click the “Apply” button. To save the query settings, hit the “Add/Update Query.”

The Related Siblings Macro is similar to the Related Item Macro, but it displays siblings. For example, you have the “Country > Tours” connection. The Related Item Macro can show the children tours of the country in the Listing Grid on the Country Single Page, and the Related Siblings Macro can display the related tours on the Tour Single Page.

Go to WordPress Dashboard > JetEngine > Query Builder, and hit the “Add New” button.

Give your query a Name. Select the type of sibling items you want to display in the Query Type field (e.g., “Posts Query” for posts).

In the General section, specify the Post Type.

query for related siblings

Then, we open the Post & Page section, click the “coin stack” icon near the Post In field, and select the “Related Siblings” option.

related siblings macro

Then, set the macro options as per your needs. The settings of this macro are similar to the previous one. For our cause, we select the name of the needed relation in the From Relation field, the “Child Object” option (because we want to display tours that are children items in our relation), and the “Current Object ID” option (because we wish that users see the related tours of the tour which post they visit), and push the “Apply” button.

related siblings macro for the post types

Click the “Add Query” or “Update Query” button. Then, create the Listing Item if you haven’t done it yet, or proceed to the Single Page.

single page for tours

Place the Listing Grid widget there, open the Custom Query section, enable the Use Custom Query toggle, and select the name of the needed Custom Query. As you can see, the tour on the Single Page is for Argentina, so the Listing Grid with the query also shows the related tours for Argentina, their same parent.

listing grid for the related siblings

After you finish editing, hit the “Publish” or “Update” button.

This macro gets the grandparents from the object. Before setting the query, be aware that you build a relationship with the Parent relation option enabled.

parent relation

It means that you have 2 relations, one is a “Parent,” another one is a “Child.” For example, Continents to Countries and Countries to Tours. The second one should have the Parent relation option enabled.

relationships scheme

Now, it’s time to build a query that will join those two and let us retrieve grandparents via grandchildren and vice versa.

Go to JetEngine > Query Builder, and hit the “Add New” button.

Give your query a Name. Select the type of grandparent items you want to display in the Query Type field. Above, we have described how to set the different query types. For our example, we have selected the “Post Query” option.

Click on the macros icon near the needed field and select the “Related Grandparents” option.

related grandparents macro

Then, set the macro options as per your needs. For our case, we have selected the name of the relationship where a grandchild to the “Continent > Country” relation is (“Continent” is a grandparent post type), and the “Current Object ID” option (because we want to display the continent that is a grandparent to the grandchild tour on the Single Page of the tour). If you want to know more about the Grandchild Object ID From options, proceed to the Related Items Macro section in this tutorial and read about the Initial Object ID From option; they have the same settings.

related grandparents macro settings

Click on the “Add Query” or “Update Query” button. Then, create the Listing for the grandparent items if you haven’t done it yet, and proceed to the grandchild Single Page.

single page for tours

Place the Listing Grid widget with the grandparent items there. Open the Custom Query section in the Listing Grid widget, enable the Use Custom Query toggle, and select the name of the needed Custom Query. As you can see, this widget with query shows the grandparent continent of the grandchildren tour.

grandchild and grandparent posts

After you finish editing, hit the “Publish” or “Update” button.

This macro gets the grandchildren from the related object. Before setting the query, be aware that you build a relationship with the Parent relation option enabled.

parent relation

It means that you have 2 relations, one is a “Parent,” another one is a “Child.” For example, Continents to Countries and Countries to Tours. The second one should have the Parent relation option enabled.

relationships scheme

Now, it’s time to build a query that will join those two and let us retrieve grandchildren via grandparents and vice versa.

Go to JetEngine > Query Builderand hit the “Add New” button.

Give your query a Name. Select the type of grandchildren items you want to display in the Query Type field. Above, we have described how to set the different query types. For our example, we have selected the “Post Query” option.

Click on the macros icon near the needed field and select the “Related Grandchildren” option.

related grandchildren macro

Then, set the macro options as per your needs. For our case, we have selected the name of the relationship where a grandchild to the “Country > Tours” relation is (“Tours” is a grandchild post type) and the “Current Object ID” option (because we want to display the tours that are grandchildren to the grandparent continent on the Single Page of the continent).

related children macros settings

Click on the “Add Query” or “Update Query” button. Then, create the Listing for the grandchildren items if you haven’t done it yet, and proceed to the grandparent Single Page.

Place the Listing Grid widget with the grandchildren items there. Open the Custom Query section in the Listing Grid widget, enable the Use Custom Query toggle, and select the name of the needed Custom Query.

custom query for the listing grid

As you can see, this widget with query shows the grandchildren tours of the Europe grandparent continent.

grandparent and grandchildren posts on the front end

That’s it. Now you know how to display the WordPress related parent, children, sibling, and grandparent posts, terms, users, and CCT items with the help of macros and the JetEngine Query Builder feature.

Was this article helpful?
YesNo

Need help?

Ask Facebook community

Get answers from 22K Crocoblock Community experts from all over the world.

Start a live chat

Ask question to support agent and share its details via chat messages.