Hi Luo,
I have this code now but it doesn't load the ads:
<div class="ads">
Ads
<?php
$url_arg = sanitize_text_field( $_GET['_sfm_wpcf-job-profession'] );
$args = array(
'post_type' => 'ad',
'meta_key' => '_sfm_wpcf-ad-profession',
'meta_value' => $url_arg,
);
$posts_array = get_posts( $args );
foreach ( $posts_array as $post ) {
echo '<pre>';
var_dump( $post );
echo '</pre>';
}
?>
</div>
I think i'm not using the right names for the custom fields. I'm using post reference fields. Do i need to remove the _sfm_ ? I already tried some options but no ads are showing.
Thanks,
Menno
1) Yes, you need to remove the _sfm_, I am not sure where it comes from.
If the custom field "job-profession" is created with Toolset Types plugin, the field slug will be "wpcf-job-profession", so this line:
'meta_key' => '_sfm_wpcf-ad-profession',
should be :
'meta_key' => 'wpcf-ad-profession',
2) As I mentioned in my previous answer:
https://toolset.com/forums/topic/filter-view-by-text-instead-of-id/#post-1314451
the custom field "Job - Profession" is registered in post type "jobs", not in "ADs" posts, so your custom codes won't work too, it won't be able to return any result.
It won't work: query "ADs" posts, and filter by fields of other post type "jobs".
As a workaround, you can consider these:
- registered custom field "Job - Profession" to both post type: ADs and Jobs,
- Edit each ADs post, setup value in field "Job - Profession",
Then you can retrieve the related posts by field "Job - Profession", and it does not need custom codes, just a post view.
Hope it is clear.
Hi Luo,
Thanks for your response. I tried some things in the code and now it works:
function lifejobs_ads() {
$key_profession = sanitize_text_field( $_GET['_sfm_wpcf-job-profession'] );
$metafield_profession = types_render_field('wpcf-ad-test');
$args = array(
'post_type' => 'ad',
'posts_per_page' => '5',
'meta_key' => $metafield_profession,
'meta_value' => $key_profession
);
// Custom query.
$query = new WP_Query( $args );
// Check that we have query results.
if ( $query->have_posts() ) {
// Start looping over the query results.
while ( $query->have_posts() ) {
$query->the_post();
?>
<div style="float: left; width: 100%;">
<?php the_title(); ?>
</div>
<?php
}
}
// Restore original post data.
wp_reset_postdata();
}
add_shortcode( 'lifejobsads', 'lifejobs_ads' );
The problem was that i used a post reference field instead of a normal field in this line:
$metafield_profession = types_render_field('wpcf-ad-test');
Is there a way to get the value of a post reference field? I would like to get the value of the post type Profession. Something like:
$post_ids = toolset_get_related_posts($company_post_id, 'ads-profession', 'parent');
Thanks,
Menno
The custom post reference field is based on one-to-many relationship, so each ADs post is also a child post of Job post.
You can follow our document to setup your custom PHP codes:
function toolset_get_related_posts()
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts
Using The New Post Relationships
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/how-to-migrate-your-site-to-new-post-relationships/#using-the-new-post-relationships
Since we don't provide custom codes support, but I can setup a demo for you, if you need more assistance for it, please provide your website credentials + ftp access in below private message box, also point out:
- the problem page URL, how do you pass the URL parameters
- where I can edit your PHP codes
Thanks
You don't need to create custom shortcode, instead, we can change the filters of post view "Ads - SERP".
I have done below modification in your website, edit theme file "functions.php", add below PHP codes:
add_filter( 'wpv_filter_query', function($query_args, $view_settings, $view_id){
if($view_id == 1321 ){
if(isset($GLOBALS['wp_query']->query_vars['post__in']) && is_array($GLOBALS['wp_query']->query_vars['post__in'])){
$post_in = $GLOBALS['wp_query']->query_vars['post__in'];
$ad_ids = array();
foreach($post_in as $pid){
$query = new WP_Query(
array(
'post_type' => 'ad',
'posts_per_page' => -1,
'toolset_relationships' => array(
'role' => 'child',
'related_to' => $pid,
'relationship' => 'ad-job-post',
),
'fields' => 'ids',
)
);
$id = $query->posts;
if(isset($id[0])){
$ad_ids[] = $id[0];
}
}
$query_args['post__in'] = $ad_ids;
}
}
return $query_args;
}, 99, 3);
Please test again
hidden link
check if it is what you want, thanks
Hi Luo,
Thanks for this. How is the ad related to the filters now? If i edit this hidden link none of the custom fields have a value?
I did not use those custom ADs fields in filter, since it will make things very complicated, instead, I use the Jobs posts of archive page results as filter, here are details:
1) Get Jobs post's IDs in that archive page
2) Use Jobs post's ID to get all related ADs post's IDs
3) Use ADs post's IDs to add a filter in the post view "Ads - SERP".
More help:
https://developer.wordpress.org/reference/classes/wp_query/#post-page-parameters
post__in (array) – use post ids. Specify posts to retrieve.
I have done a little fix and add comments in above custom PHP codes, now, it is below:
add_filter( 'wpv_filter_query', function($query_args, $view_settings, $view_id){
if($view_id == 1321 ){
if(isset($GLOBALS['wp_query']->query_vars['post__in']) && is_array($GLOBALS['wp_query']->query_vars['post__in'])){
// Get Jobs post's IDs in that archive page
$post_in = $GLOBALS['wp_query']->query_vars['post__in'];
$ad_ids = array();
//Use Jobs post's ID to get all related ADs post's IDs
foreach($post_in as $pid){
$query = new WP_Query(
array(
'post_type' => 'ad',
'posts_per_page' => -1,
'toolset_relationships' => array(
'role' => 'child',
'related_to' => $pid,
'relationship' => 'ad-job-post',
),
'fields' => 'ids',
)
);
$ids = $query->posts;
$ad_ids = array_merge($ad_ids, $ids); //
}
//Use ADs post's IDs to add a filter in the post view "Ads - SERP".
$query_args['post__in'] = $ad_ids;
}
}
return $query_args;
}, 99, 3);
For your reference.
Hi Luo,
2) Use Jobs post's ID to get all related ADs post's IDs
I don't understand the logic of the relation between the jobs and ads now.
So why am i seeing 'Test Ad Luo' when i choose 'Developer' when i filter here hidden link? How do i say now: ad x belongs to profession filter x?
Q1) I don't understand the logic of the relation between the jobs and ads now.
I have already mentioned in previous answer:
https://toolset.com/forums/topic/filter-view-by-text-instead-of-id/page/2/#post-1316099
The custom post reference field is based on one-to-many relationship, so each ADs post is also a child post of Job post.
So you can get related "ADs" posts with Job post ID
Q2) So why am i seeing 'Test Ad Luo' when i choose 'Developer' when i filter here...
Please edit the AD post "Test Ad Luo", in section "Ad - Job post*", you are using below option: Testjob 12
So AD post "Test Ad Luo" will be able to display in above URL you mentioned above:
hidden link
Since there is "Testjob 12" in the result, so the related AD post "Test Ad Luo" display in AD list.
Hope it is clear.
I'm sorry, i didn't see the Post job field.. I think it works great now!
Really thanks for your awesome support, and sorry for taking a lot of your time.. 🙂
Thanks,
Menno
You are welcome, please feel free to create new ticket if there is other questions. thanks