Query in Relationship - Post per page
Support › MB Relationships › Query in Relationship - Post per pageResolved
- This topic has 9 replies, 2 voices, and was last updated 3 years, 9 months ago by
Prabakaran Shankar.
-
AuthorPosts
-
June 26, 2021 at 5:37 PM #29137
Prabakaran Shankar
ParticipantI 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.June 27, 2021 at 2:05 PM #29149Long Nguyen
ModeratorHi,
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.
June 27, 2021 at 10:39 PM #29155Prabakaran Shankar
ParticipantHello,
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: 69Refer to the image output
https://1drv.ms/u/s!AjW7znckfYbzg9J7VsHKVa64gaywXg?e=BakJ7Mhowever, 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=ktfDY7June 28, 2021 at 11:09 AM #29159Long Nguyen
ModeratorHi,
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
toemp
and change the relationshipfrom
toto
{% 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
June 28, 2021 at 12:04 PM #29161Prabakaran Shankar
ParticipantHello,
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?
June 28, 2021 at 3:58 PM #29167Long Nguyen
ModeratorHi,
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 variablejob_args
{% set job_args = { post_type: 'job', posts_per_page: 5, relationship: {id: 69, to: employer.ID} } %}
June 28, 2021 at 9:59 PM #29179Prabakaran Shankar
ParticipantThank 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.June 28, 2021 at 11:04 PM #29187Prabakaran Shankar
ParticipantIs that any other way to use the query in the relationship attribute?
set args is not working with relationships in my case.June 29, 2021 at 9:46 AM #29196Long Nguyen
ModeratorHi,
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.
July 1, 2021 at 1:01 PM #29229Prabakaran Shankar
ParticipantThank you So much...
-
AuthorPosts
- You must be logged in to reply to this topic.