Support

Account

Forum Replies Created

  • To actually make it a little harder, I need to add one more detail. The product_cat categories aren’t obviously Black/White. I used those as a minimum reproducible example. The actual situation is a bit harder, in that there are two top-level categories again let’s say that their values are Black/White, but the user will be selecting one level deeper than that. So the mechanism will need to check the selected category and go one level up to find whether the selected cat has Black or White as its parent…

  • WOW John!!! I had never noticed that 3rd parameter. Indeed that was exactly what I wanted! If only I had your answer yesterday, I wouldn’t have resourced in directly querying the DB…

    $_poster = $wpdb->get_results("SELECT meta_value FROM {$wpdb->prefix}postmeta WHERE meta_key = 'movie_poster' AND post_id = {$post->ID};", ARRAY_A)[0]['meta_value'];

    But I’ll update my code as your solution is much more clean! Thanks a lot!

  • OK, after two days I finally found what was going on… Well, I was inspired by this post in WP reference (such a precious place of knowledge)…

    As I said in the first post, I used the do_action( “save_post_{$post->post_type}”, int $post_ID, WP_Post $post, bool $update ) hook point. And according to the post above, this hook fires before the generic do_action( ‘save_post’, int $post_ID, WP_Post $post, bool $update ), so what I was saving in my hook point (in regard to the ACF fields) was overwritten when the generic hook was firing…

    So I used the generic one instead, and did a check where this was true $post->post_type == ‘movie’

    That was all that was needed! No need priority settings, nothing… Just this was enough to solve my problem!!! Amazing!

  • I think I found out (as an educated guess) what is happening… I’m hooking into WP’s stock save_post_{$post->post_type}

    But somewhere in there, ACF’s similar hook point fires up and tries to update the fields I’ve already updated in my own hook, with the values of the respective input fields (which are effectively empty) thus replacing my fields with an empty string…

    I somehow need to remove/prevent from within my hook ACF’s hook from firing up… How do I do that? I suppose remove_action(‘acf/save_post’) won’t be enough…

  • Actually, after taking a look at the WP code reference of wp_safe_redirect() it is specifically advised that wp_safe_redirect() should almost always be followed by an exit; command…

    Well this was enough to fix my problem.

    Of course this isn’t a proper fix, that would essentially stop the data data overwriting from happening, but it’s a workaround that at least solves my problem…

    If anyone has any proper fix/suggestion, please let me know!

  • @hube2 , John, sorry for hi-jacking the thread with this off-topic, I’m totally clueless about the issue below, and for a fact I know that if anyone is to help me figure it out, most probably that one will be you.

    So I noticed that 3-4 out of 10 times I make some edits in a post (any post) that has an ACF multi-date picker, after the post is re-published, the date picker calendar will lose its localization, until I interact somehow with it. Once I change the month, select an unselected date, or unselect a selected one, the calendar magically gets localized again. And because in my localization settings, Monday is starting the week, even that small detail gets changed as well… And ALL that is not happening every time… As I said, somehow it’s happening 3-4 times out of 10 maybe…

    In the video below, until @1:30 I published the post 3-4 times and the calendar didn’t lose its localization. Then suddenly, on @1:30, it lost it two times in a row (where you can see all things I described above). Then, at the end of the video, again it didn’t lose it after one more re-Publish…

    Any idea why this may be happening? It’s not super serious, as it’s happening in the backend, but still, I’d like to figure out why!

    https://www.youtube.com/watch?v=yKodqaCkrsg

  • @hube2 thanks a ton for your reply. Truth is I solved this earlier today, slightly differently than your proposed solution, but still it’s solved. I’ll post the code that I used, and I’ll elaborate on it later.

    add_filter('acf/fields/post_object/result/name=screening_cinema', 'add_parent_to_cinema_options', 10, 4);
    function add_parent_to_cinema_options($text, $post, $field, $post_id)
    {
        if ($post->post_parent) {
            $text .= ' | ' . __('Parent', 'acf') . ': ' . get_the_title($post->post_parent);
        }
    
        return $text;
    }
    
    add_action('acf/input/admin_footer', 'add_js_for_disabling_cinema_options');
    function add_js_for_disabling_cinema_options() {
        ?>
        <script type = "text/javascript">
        (function($) {
            acf.add_filter('select2_ajax_results', function(json, params, instance) {
                if (instance.data.field.data.name === 'screening_cinema') {
                    json.results.forEach(function(element) {
                        if (typeof element.description === "undefined") {
                            element.disabled = "true";
                        }
                    });
                }
    
                return json;
            });
        })(jQuery);
        </script>
        <?php
    }

    First of all, I noticed in includes/fields/class-acf-field-post_object.php at lines ~259-275 the following piece of code:

    		// vars
    		$result = array(
    			'id'	=> $id,
    			'text'	=> $text
    		);
    		
    		
    		// look for parent
    		$search = '| ' . __('Parent', 'acf') . ':';
    		$pos = strpos($text, $search);
    		
    		if( $pos !== false ) {
    			
    			$result['description'] = substr($text, $pos+2);
    			$result['text'] = substr($text, 0, $pos);
    			
    		}

    According to this, it’s possible to add a description and a text param in the json object used in acf.add_filter(‘select2_ajax_results’, function(json, params, instance)

    That’s why I used the filter add_filter(‘acf/fields/post_object/result/name=screening_cinema’) to first add the parent part in the text shown, and then in the JS function I searched for the existence of the description param to decide whether I’ll disable the option or not.

    Different approach than yours, the same effect and maybe a bit faster, as I’m not creating that whole bunch of top-category IDs. Plus, I liked that for the selected option, I get this | Parent: <parent title> in the dropdown so that I know exactly what parent the selection belongs to.

    screenshot

    PS: The website is localized to my language that’s why you see that ” | Γονέας:” term in place of the ” | Parent:”.

    Anyway, thanks a million for your help! Regards.

  • @radgh (or anyone else) could you help me with my situation please, which is even simpler than yours but still I haven’t been able to fully figure it out…

    So, in my case I only use one Post Object (cousin of Relationship), that pulls data from a hierarchical (parent-child) CPT. Very simple and stock up to here. Ah, the Post Object is within a Repeater, so that the admin will be able to add rows of Post Objects, but that doesn’t affect the case anyway.

    The problem is I want to prevent the admin who will use the system from choosing top categories (where parent=0), but rather choose only child categories, by disabling the top categories within the select2.

    At this point let me tell you, in case you don’t already know, that it’s a select2 stock functionality to disable specific options by adding a property disabled:true for that option.

    So, I did a lot of research and found that the only JS filter that is being triggered every time the select2 gets populated is acf.add_filter(‘select2_ajax_results’, function( json, params, instance ) (https://www.advancedcustomfields.com/resources/javascript-api/#filters-select2_ajax_results), where json param holds only the id and text properties of the results.

    For testing purposes I added a third param disabled:true to each option, and indeed all options where greyed out…

    add_action('acf/input/admin_footer', 'my_acf_input_admin_footer');
    function my_acf_input_admin_footer() {
        ?>
        <script type = "text/javascript">
        (function($) {
            // JS here
            acf.add_filter('select2_ajax_results', function(json, params, instance) {
                $.each($(json.results), function() {
                    $(this).prop("disabled", true);
                });
                return json;
            });
        })(jQuery);
        </script>
        <?php
    }

    The problem is I don’t know at the time select2_ajax_results filter gets triggered which options are top categories and which are children. So, I need somehow to add a third param, like top:true, to json at an earlier stage. But what filter would that be? Probably it should be server side, but what exactly filter?

    The following screenshot was taken with the test code above in place, which disabled ALL options…

    Screenshot

    Any insight would be greatly appreciated.

  • OK, I found it myself! All I needed was ACF Pro, and its Repeater field where I’d set up three more fields: Cinema (a Relationship to Theater), a Time picker, and a Multi-date picker!

Viewing 9 posts - 1 through 9 (of 9 total)