Support Forum
Hello,
I am a total newbie at Metabox, as I am transitioning from Toolset, this might be a totally stupid question, but I haven’t managed to find a solution to this.
I am developing a website for my company’s Professional Training Academy with Metabox and Breakdance, and I set two different custom post types:
A) COURSE CATALOG - Where I store all the kinds of courses the academy offers (I.e. HACCP, FIRE SAFETY, FIRST AID etc).
B) COURSE SESSION - which is a child of course catalog, and it stores all the information about a specific starting course (i.e. HACCP course starting on September 1st)
In order to avoid having to set a featured image for a course session every time I insert one (they would be repetitive), I have set up a custom field in the corresponding COURSE CATALOG, and I would like to display the course cover (single image field) of the parent post when I output course sessions.
I would like to set a section in my home page, with a shortcode displaying all the starting courses (filtered by start date >= today) styled as bootstrap cards with parent cover image on top and course session info on bottom.
I managed to get this to list all the starting courses, but I cannot find a way to retrieve the cover image from the parent.
{% set args = { post_type : "sessione-corso" , posts_per_page: -1, order : "DESC" } %}
{% set posts = mb.get_posts (args) %}
{% set currentDate = "now"|date("Y-m-d") %}
{% if field | date( 'Y-m-d' ) >= currentDate %}
{% endif %}
{% endfor%}
I decided to do this via MB VIEWS shortcode as I wouldn’t know how to properly filter posts via a post loop builder component… but I am up for suggestions about better ways to achieve this.
Thank you!
Hello,
How do you set a Course Catalog as a parent post of a Course Session? Do you use the extension MB Relationships?
Hello, thanks for answering.
Yes I set it thru MB relationships extension.
Hello,
So you can use another query to get the related catalog post from the session post ID. For example:
{% for post in posts %}
{# get the course catalog post #}
{% set catalog_args = { post_type: 'course_catalog', relationship: { id: 'catalog-to-session', to: post.ID } } %}
{% set catalog_posts = mb.get_posts( catalog_args ) %}
{% for catalog_post in catalog_posts %}
{# get the catalog post image and other info #}
{% endfor %}
{% endfor %}
You will need to change the post type and relationship ID with your own, just for example.
Thanks for your help.
Could you be more specific giving me an example of the syntax needed to get a field named "cover_corso" please? So I can adapt this to any field I need from the parent post.
Thanks
Please discard previous message, I hadn't seen that the code was right there.
So this is the complete code, but it's still not working. Can you please point out any mistakes you see?
{% set args = { post_type : "sessione-corso" , posts_per_page: -1, order : "DESC" } %}
{% set posts = mb.get_posts (args) %}
{% set currentDate = "now"|date("Y-m-d") %}
{% for post in posts %}
{% endfor %}
<ul>
{% for post in posts %}
{# get the course catalog post #}
{% set catalog_args = { post_type: 'catalogo-corso', relationship: { id: 'sessione-corso-a-catalogo', to: post.ID } } %}
{% set catalog_posts = mb.get_posts( catalog_args ) %}
{% for catalog_post in catalog_posts %}
{% set covercorso = attribute( post, 'cover-corso' ) %}
{# get the catalog post image and other info #}
{% endfor %}
{% set field = attribute( post, 'data-partenza-corso' ) %}
{% set prezzo_associati = attribute( post, 'prezzo-associati' ) %}
{% if field | date( 'Y-m-d' ) >= currentDate %}
<div class="container">
<div class="cardcorso">
<div class="imagecorso">
<img src="{{ coverfoto.full_url }}" width="150" />
<div class="badge">€ {{ prezzo_associati }},00</div>
</div>
<div>
<h4>{{ post.title }}</h4>
Data Partenza: <strong>{{ field | date( 'd-m-Y' ) }}</strong>
{% set field = attribute( post, 'calendario-corso' ) %}
<h5>CALENDARIO CORSO</h5>
<ul>
{% for clone in field %}
<li> {{ clone | date( 'd-m-Y' ) }}</li>
{% endfor %}
</ul>
</div>
</div>
</div>
{% endif %}
{% endfor%}
</ul>
Hello,
You said the field ID is cover_corso
but you added the field ID in your code: cover-corso
. The attribute of the catalog_post
, not post
. And I also do not see the code to output the image in the catalog post loop.
If you are not able to complete the task, please contact us here https://metabox.io/contact/
Our development team will help you with an extra fee.
If possible I would like to get to the bottom of this with your help, because it's in my opinion a crucial step on the path to learning to use METABOX to its full capacity.
So I corrected all the errors you pointed out, double checking names and slugs.
Recapping:
Course Catalog Slug : corso
Relationship Slug : sessione-corso-a-catalogo
Course Session Slug : sessione-corso
Now my view code is :
{% set args = { post_type : "sessione-corso" , posts_per_page: -1, order : "DESC" } %}
{% set posts = mb.get_posts (args) %}
{% set currentDate = "now"|date("Y-m-d") %}
{% for post in posts %}
{# get the course catalog post #}
{% set catalog_args = { post_type: 'corso', relationship: { id: 'sessione-corso-a-catalogo', to: post.ID } } %}
{% set catalog_posts = mb.get_posts( catalog_args ) %}
{% for catalog_post in catalog_posts %}
{# get the catalog post image and other info #}
{% set coverfoto = attribute( catalog_post, 'cover-corso' ) %}
{% set duratacorso = attribute( catalog_post, 'durata-corso' ) %}
{% endfor %}
{# get the session course info #}
{% set datapartenza = attribute( post, 'data-partenza-corso' ) %}
{% set prezzo_associati = attribute( post, 'prezzo-associati' ) %}
{% if datapartenza | date( 'Y-m-d' ) >= currentDate %}
{# display the course card with data from both PARENT CORSO and SESSIONE CORSO #}
<div class="container">
<div class="cardcorso">
<div class="imagecorso">
<div class="badge">€ {{ prezzo_associati }},00</div>
</div>
<div>
<h4>{{ post.title }}</h4>
Data Partenza: {{ datapartenza | date( 'd-m-Y' ) }}
{% set calendariocorso = attribute( post, 'calendario-corso' ) %}
<h5>CALENDARIO CORSO</h5>
{% endfor %}
<h5>DURATA CORSO</h5>
<p>{{ duratacorso }}</p>
</div>
</div>
</div>
{% endif %}
{% endfor%}
Still it is not showing neither the "cover_corso" as image nor the duration of the course {{ duratacorso }}
If I inspect the page I see this:
html code where the image should be ->
html code where the course duration should be=> <p></p>
So it seems that the second query is not targeting anything. Please tell me what I am doing wrong
or what should I check for errors.
Thank you!