I have a multisite install with a custom post type called “promos” that exists on the main site, or blog_id 1. All of the subsites are going to be “locations” with blog_id serving as a corporate site. In the options panel of each “location” site, we have an area that shows a an ACF checkbox for available “promos.” This has been done in the past using the following code:
//ACF For Promos Checkbox
add_filter(‘acf/load_field/name=promos’, function($field) {
switch_to_blog(1);
$posts = new WP_Query([
‘post_type’ => ‘promos’,
‘post_status’ => array(‘publish’),
‘posts_per_page’ => -1
]);
$choices = [];
while ($posts->have_posts()) {
$posts->the_post();
$choices[get_the_ID()] = get_the_title();
}
wp_reset_query();
restore_current_blog();
$field[‘choices’] = $choices;
return $field;
});
As you can see we are getting the post_title from the corporate blog. All of this works fine. Now we are trying to add a new text field next to these checkboxes that allow us to add a custom promo title. In other words, if a “location” selects one of the “promos,” they will be able to add a custom title as well. On the front end, it will output the promo they selected (pulling from blog_id 1), but also add in their new custom title from the ACF text field.
Some code used for displaying the promo would be helpful.
Without that.
1) Create another field to hold the custom title, make it conditional on the select field having a value
2)
$custom_title = get_field('custom_title_field');
// maybe get the promo to show
// somewhere in that code
if (!empty($custom_title) {
echo $custom_title;
} else {
echo $whatever_should_be_shown_normally;
}
The code I have is above. It is what we are using to pull the post from blog_id and have it output on the options page. I attached a screen shot. In the screen shot, you can see the option to toggle it on. Which is fine but we also need two text fields to show next to it that a location can add their own text that is tied to that selected option from blog_id 1.
That code is for populating the choices of a select field, it has nothing to do with the front end display.
The “custom title” needs to be a different text field. You can make it conditional on the checkbox field having a value.
John
Yes I have the front end display taken care of. We use the code I have for other items. What is unique with this option, is added in two additional text fields with it. Those arent pulling from blog_id one.
So your saying to use the code I have and then add a condition after the restore blog that outputs the two new fields if it is checked? I need those two new fields to have updated field_id’s for each option. So if we have 3 promos, I will have a total of 6 text fields, two for each available post from blog id 1.
So, if you want to relate a title with a promo….. you’ll need to do something different. Don’t ask me why, but I was thinking that each site could only select one promo and I should have realized that was not the case because you said checkbox. Sometimes I’m a little slow.
I am assuming that
The only way that I can think of do to this is to completely change what you are doing and use a repeater field instead of a checkbox field to select all posts. One sub field would be a radio or select field that only allows one selection and a second sub field to provide a custom title for each individual selection. There would be no other way to associate each title with a specific post.
Yes you are correct. The main blog is what is setting the title of the promo and is what is showing on each sites options page. This is what tells the location the name of the promo, for example, Mothers Day Promo. There may be two additional that are called Buy One Get One, One Free Week. When a location selects that option, two new fields will show that allow them to enter a unique title to be shown on the front end of their page and the other field might be for a link to add their unique booking link. Does that help?
The code I gave has worked in the past because we just needed to output a template that was hardcoded based on a selection of the checkbox. Now we need to allow a couple of fields to go with those selections.
You must be logged in to reply to this topic.
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.