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.
Related Items Macro
If you created a parent-child relationship and want to show related items – this macro is for you.
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.
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.
Open the Post & Page section, click on the macros icon near the Post In field and select the “Related Items” option.
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.
We have set such a query for the relation in this tutorial. After you finish, click on the “Apply” and “Add Query” buttons.
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.
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.
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.
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.
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.
Fill it with content with the help of, for example, Dynamic Field or other dynamic widgets.
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.
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.
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.
Custom Content Type Query
If you decide to display related Custom Content Type items, select the “Custom Content Type Query” Type. Select the name of needed CCT in the From Content Type field, and hit the “Add new” button in the Query section.
In the new window, pick the “Item ID” option in the Field and the “Equal (=)” in the Compare. Hit the macros 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 on the “Apply” and “Add Query” buttons.
Related Siblings Macro
The Related Siblings Macro is similar to the Related Item Macro, but it displays not the item’s parent or children but 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 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. We have described above how to set the different query types for such macros. For our example, we have selected the “Post Query” option, but choose the proper type of query if you want to show the sibling terms, users, or CCT items.
In the General section, specify the Post Type.
Open the Post & Page section, click on the macros icon near the Post In field and select the “Related Siblings” option.
Then, set the macro options as per your needs. The settings of this macro are similar to the previous one. For our cause, we have selected the name of needed relation, 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).
Click on 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.
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.
After you finish editing, hit the “Publish” or “Update” button.
Related Grandparents Macro
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.
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.
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.
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.
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.
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.
After you finish editing, hit the “Publish” or “Update” button.
Related Grandchildren Macro
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.
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.
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 Builder, and 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.
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).
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.
As you can see, this widget with query shows the grandchildren tours of the Europe grandparent continent.
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.