Thank you John! I managed to figure this out myself after some trial and error. My solution for anyone else who may be trying to get started with this:
Step 1: create a message field for the product buy link and add an empty element to hold the text. In my case I used <code id="course--checkout_link"></code>
Step 2: Add the following code to your functions.php
file
`
add_action(‘acf/input/admin_footer’, ‘my_acf_input_admin_footer’);
function my_acf_input_admin_footer() {
// Only output this for my_courses
if ( ‘my_courses’ == get_post_type() ) {
?>
<script type=”text/javascript”>
(function($) {
// Get the site URL
var origin = window.location.origin; // Returns base URL (https://example.com)
// Print the URL on page load
acf.add_action(‘ready’, function( $el ){
// Get select value
var currentOption = $(‘select#acf-field_5e69a22520434-field_5e69a28620436’).children(“option:selected”).val();
$(‘#course–checkout_link’).text( origin + ‘/checkout/?add-to-cart=’ + currentOption );
});
// Update the URL when a new option is selected
$(‘select#acf-field_5e69a22520434-field_5e69a28620436’).change(function(){
var selectedOption = $(this).children(“option:selected”).val();
$(‘#course–checkout_link’).text( origin + ‘/checkout/?add-to-cart=’ + selectedOption );
});
})(jQuery);
</script>
<?php
}
}
`
Thanks to the support team, I got this to work. You don’t need to reference $post_id
at all. Here’s my code:
add_filter('acf/fields/post_object/query', 'prefix_only_published_posts', 10, 3); /** * Display only published posts */ function prefix_only_published_posts( $args, $field, $post_id ) { $args['post_status'] = 'publish'; $args['orderby'] = 'date'; $args['order'] = 'DESC'; return $args; }
It was affecting my staging server too, but looks like the 5.2.6 update fixed it. Thanks!
I was looking for this too. In the end I discovered on the ACF → Export page:
Export to PHP
ACF will create the PHP code to include in your theme.
Registered field groups will not appear in the list of editable field groups. This is useful for including fields in themes.
Please note that if you export and register field groups within the same WP, you will see duplicate fields on your edit screens. To fix this, please move the original field group to the trash or remove the code from your functions.php file.
Select field group(s) from the list and click “Create PHP”
Copy the PHP code generated
Paste into your functions.php file
To activate any Add-ons, edit and use the code in the first few lines.
Hope that helps!
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.