Support Forum
Support › MB Relationships › Query in Relationship - Post per pageResolved
I have created a relation from post (job) to page (emp/employer). Next, using MB Views, I have developed a page template with other data. along with that, wish to show the recent jobs by the employer.
According to the below documentation, I have set the posts to display to 10 per page. But there is no result.
https://metabox.io/mb-views-how-to-display-relationships/
{% set args = {post_type: 'job', posts_per_page: 5} %}
{% set jobs = mb.get_posts( args ) %}
{% set relationship = attribute( relationships, '69' ) %}
{% for post in relationship.from %}
<ol>
<li><a href="{{ post.url }}">{{ post.title }}</a> (Deadline: {{ post.j_deadline | date( 'M j, Y' ) }})</li>
</ol>
{% endfor %}
{{ mb.get_the_posts_pagination() }}
Please look at the image for the output. I have placed the code in part of the page. this may work well on the archive page, but working on the part of the page. (https://docs.metabox.io/extensions/mb-views/)
Also, please help me to make the ordered list.
Hi,
To get the employer connected from the job, you need to create a loop to get the relationship nested in the loop that gets the job. Please check this code
{% set args = { post_type: 'job', posts_per_page: 5 } %}
{% set jobs = mb.get_posts( args ) %}
{% for job in jobs %}
<a href="{{ mb.get_the_permalink( job.ID ) }}">{{ job.post_title }}</a>
{% set emp_args = { post_type: 'employer', nopaging: true, relationship: {id: 'job_to_employer', from: job.ID} } %}
{% set employers = mb.get_posts( emp_args ) %}
{% for employer in employers %}
<a href="{{ mb.get_the_permalink( employer.ID ) }}">{{ employer.post_title }}</a>
{% endfor %}
{% endfor %}
remember to correct the slug of CPT job, employer, and the relationship ID with your own.
Hello,
Thank you for your workout to show the connected jobs.
But, It shows only the recent post in the CPT job instead of the post related to the relationship.
I have used the following code.
{% set args = { post_type: 'job', posts_per_page: 5 } %}
{% set jobs = mb.get_posts( args ) %}
{% for job in jobs %}
<a href="{{ mb.get_the_permalink( job.ID ) }}">{{ job.post_title }}<br></a>
{% set emp_args = { post_type: 'emp', nopaging: true, relationship: {id: '69', from: job.ID} } %}
{% set employers = mb.get_posts( emp_args ) %}
{% for employer in employers %}
<a href="{{ mb.get_the_permalink( emp.ID ) }}">{{ emp.post_title }}</a>
{% endfor %}
{% endfor %}
CPT Plural:"Employers"; Singular:"Employer";URL:"emp";
CPT Plural:"Jobs"; Singular:"Job";URL:"job";
Relationship id: 69
Refer to the image output
https://1drv.ms/u/s!AjW7znckfYbzg9J7VsHKVa64gaywXg?e=BakJ7M
however, if I use query following general code, it shows all the related post
{% set relationship = attribute( relationships, '69' ) %}
{% for post in relationship.from %}
<a href="{{ post.url }}">{{ post.title }}</a><br>
{% endfor %}
Refer to the image output
https://1drv.ms/u/s!AjW7znckfYbzg9J8ghM4PI4UJfQS2w?e=ktfDY7
Hi,
Can you please clarify that you want to show a list of employers then show recent posts by each employer?
If yes, you can just switch post type job
to emp
and change the relationship from
to to
{% set args = { post_type: 'emp', posts_per_page: 5 } %}
{% set employers = mb.get_posts( args ) %}
{% for employer in employers %}
<a href="{{ mb.get_the_permalink( employer.ID ) }}">{{ employer.post_title }}</a>
{% set job_args = { post_type: 'job', relationship: {id: 69, to: employer.ID} } %}
{% set jobs = mb.get_posts( job_args ) %}
{% for job in jobs %}
<a href="{{ mb.get_the_permalink( job.ID ) }}">{{ job.post_title }}</a>
{% endfor %}
{% endfor %}
and refer to this documentation to know how to use the custom query https://docs.metabox.io/extensions/mb-views/#custom-query
Hello,
Thank you very much.
I have used your code to display the connected jobs with the employer. But the result shows the recent jobs without any pagination.
Each job is connected with the employer. So, Each employer had many jobs. My aim is to show the jobs (5 recent jobs only) by the employer.
Along with your code, I have used the standard relationship query, which shows the jobs related to the employer.
{% set args = { post_type: 'emp', posts_per_page: 5 } %}
{% set employers = mb.get_posts( args ) %}
{% for employer in employers %}
<a href="{{ mb.get_the_permalink( employer.ID ) }}">{{ employer.post_title }}</a>
{% set job_args = { post_type: 'job', relationship: {id: 69, to: employer.ID} } %}
{% set jobs = mb.get_posts( job_args ) %}
{% for job in jobs %}
<a href="{{ mb.get_the_permalink( job.ID ) }}">{{ job.post_title }}</a><br>
{% endfor %}
{% endfor %}
<hr>
{% set relationship = attribute( relationships, '69' ) %}
{% for post in relationship.to %}
<a href="{{ post.url }}">{{ post.title }}</a><br>
{% endfor %}
<hr>
{% set relationship = attribute( relationships, '69' ) %}
{% for post in relationship.from %}
<a href="{{ post.url }}">{{ post.title }}</a><br>
{% endfor %}
you can see the result on the below page
https://nviewscareer.com/emp/technical-university-of-denmark-dtu/
https://1drv.ms/u/s!AjLgaRuebK87hfUKXWijXfv_5x5l0A?e=Xl4hhg
will query work with the relationship?
Hi,
The pagination does not work with the custom query. Refer to this topic https://support.metabox.io/topic/show-pagination-for-query/
If you want to limit 5 jobs to display, please add the argument posts_per_page
to the variable job_args
{% set job_args = { post_type: 'job', posts_per_page: 5, relationship: {id: 69, to: employer.ID} } %}
Thank you for your effort to solve it.
but it is not helpful. Query is not functioning as expected (limit post - 5).
Thank you. Please close this ticket.
Is that any other way to use the query in the relationship attribute?
set args is not working with relationships in my case.
Hi,
It's the default code to show the related objects and does not support adding more args. You need to use the custom query to set more args. Again, if you want to show 5 jobs from the current employer, please try to use this code
{% set job_args = { post_type: 'job', posts_per_page: 5, relationship: {id: 69, to: post.ID} } %}
{% set jobs = mb.get_posts( job_args ) %}
{% for job in jobs %}
<a href="{{ mb.get_the_permalink( job.ID ) }}">{{ job.post_title }}</a><br>
{% endfor %}
Just one loop and remove the parent loop that show the 5 employers.
Thank you So much...