@johanhermansson So this would just disable it from the “editor’s” view but I’d be able to change it programmatically using “update_field” if condition is met?
@hube2 Finally getting back to this, it’s been so long that I have to try and remember what I was doing.
So per my last code:
add_action( 'profile_update', 'update_user_company' );
add_action( 'personal_options_update', 'update_user_company' );
add_action( 'edit_user_profile_update', 'update_user_company' );
function update_user_company( $user_id, $args ) {
$user = get_user_by( 'ID', $user_id );
$email = $user->user_email;
$company = get_field( 'user_company', 'user_' . $user_id );
if ($company == '') {
list($user, $domain) = explode('@', $args['user_email'] );
if ($domain == 'amyling.com') {
update_field( 'user_company', 'Pearlsin Arts');
};
}
}
If I were to change ‘Pearlsin Arts’ to the post ID, would that then work or I still need query the company posts?
I got it!
I just add this as the 3rd line
$company_rtn = get_field('rtn', $user_company->ID);
@hube2 I looked at the ACF field again and noticed that user_company is actually a post object field type with a return format of post object.
How would I check if the field is empty/null before I do update_field? Would I also still need do a query of the company post?
I also tried this but doesn’t work. when I update the code in functions.php, I just go to the user’s profile in the backend and refresh the page to see if the relationship field has been updated but it stays on “Select”.
add_action( 'profile_update', 'update_user_company' );
add_action( 'personal_options_update', 'update_user_company' );
add_action( 'edit_user_profile_update', 'update_user_company' );
function update_user_company( $user_id, $args ) {
$user = get_user_by( 'ID', $user_id );
$email = $user->user_email;
$company = get_field( 'user_company', 'user_' . $user_id );
if ($company == '') {
list($user, $domain) = explode('@', $args['user_email'] );
if ($domain == 'amyling.com') {
update_field( 'user_company', 'Pearlsin Arts');
};
}
}
@hube2 I tried this but I can’t get it to work and I am not exactly sure how to go about writing this. Can you give me some pointers?
add_action( 'profile_update', 'update_user_company' );
add_action( 'personal_options_update', 'update_user_company' );
add_action( 'edit_user_profile_update', 'update_user_company' );
function update_user_company( $user_id, $args ) {
$user = get_user_by( 'ID', $user_id );
$email = $user->user_email;
$field_key = 'user_company';
list($user, $domain) = explode('@', $args['user_email'] );
if ($domain == 'amyling.com') {
$company = $user_company->post_title('Pearlsin Arts');
update_field($field_key, $company, 'user_' . $user_id);
};
}
Thank you, I will try to see if I can get this. I get the gist of it but it’s writing the function that confuses me as I’m still new to this.
After doing a var_dump on $company, the reason why echoing $compmany wasn’t pulling anythng was because it was a wp object or wp array (I’m not sure of the correct terminology.
I had to select the post_title in order for it to work. So I ended up with this:
function user_organization($atts) {
$user_id = get_current_user_id();
$user_company = get_field('user_company', 'user_' . $user_id);
echo $user_company->post_title;
}
add_shortcode('user_organization_info', 'user_organization');
I’m curious to find out if there has been any improvements on this as well?
Thank you for the code. I plugged that in and made changes as necessary and got it to work except listing the titles. It is listing the current page titles over and over instead of from the relationship field.
Do you know where I might have gone wrong?
$avail_services = get_field( 'related_services', false, false );
if ( $avail_services ) {
$types = get_terms( array(
'taxonomy' => 'service_type',
'hide_empty' => true,
'object_ids' => $avail_services
)
);
foreach( $types as $type ) {
$args = array(
'post_type' => 'services',
'posts_per_page' => -1,
'post__in' => $avail_services,
'orderby' => 'post__in',
'tax_query' => array(
array(
'taxonomy' => 'service_type',
'terms' => $type->slug,
'field' => 'slug',
),
),
);
$svc = new WP_Query($args);
echo '<h2>'.$type->name.'</h2>';
echo '<ul class="sidebar-menu">';
foreach( $svc->posts as $post ) { setup_postdata( $post );
echo '<li>'.get_the_title().'</li>';
};
wp_reset_postdata();
echo '</ul>';
};
};
Thank you for this code! It has helped with categorization, however, I am having an issue. It is not listing the correct titles under each category. Instead it’s categorizing from the relationship field but when I do the_title(); it is repeating the curent page’s title.
Custom post types named solutions and services. Services has taxonomy service_type attached.
In solutions, I have created a relationship field called related_services.
Below code is in the template for solutions page. Any idea what I might be doing wrong?
<?php
$avail_services = get_field( 'related_services', false, false );
if ( $avail_services ) :
$types = get_terms( array(
'taxonomy' => 'service_type',
'hide_empty' => true,
'object_ids' => $avail_services
)
);
foreach( $types as $type ) :
$args = array(
'post_type' => 'services',
'posts_per_page' => -1,
'post__in' => $avail_services,
'orderby' => 'post__in',
'tax_query' => array(
array(
'taxonomy' => 'service_type',
'terms' => $type->slug,
'field' => 'slug',
),
),
);
$svc = new WP_Query($args); ?>
<p class="widget-title service_types"><?php echo $type->name; ?></p>
<ul class="sidebar-menu">
<?php foreach( $svc->posts as $post ) : setup_postdata( $post ); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; wp_reset_postdata(); ?>
</ul>
<?php endforeach; ?>
<?php endif; ?>
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.