• Resolved isinica

    (@isinica)


    Hi
    Sorry for my english))
    I noticed that if I edit a previously added post, sorts in different taxonomies stop working. Post disappears from the records on the site, and also disappears in the plugin itself where their order is set. Post appears again only after clicking “Reset order”
    For a long time I could not understand what the problem was, but when studying the Reorder_Post_Within_Categories_Admin class, there is a line in the save_post method

    $ranked_ids += array_intersect($post_terms, $ranked_terms);

    and it doesn’t work properly.
    + = does not add new values to the end of the array, but writes only values for new keys. Old keys do not change
    Here is an example.
    There are three post taxonomies in which the following terms are selected
    (
    [0] => 17
    [1] => 18
    [2] => 19
    [3] => 41
    )

    (
    [0] => 56
    [1] => 24
    [2] => 25
    [3] => 40
    [4] => 29
    [5] => 30
    [6] => 27
    [7] => 31
    [8] => 33
    [9] => 26
    [10] => 37
    [11] => 39
    [12] => 35
    [13] => 38
    )

    (
    [0] => 60
    [1] => 42
    [2] => 45
    )

    at each iteration, the resulting array ($ranked_ids) looks like this:

    fitst iteration
    [0] => 17
    [1] => 18
    [2] => 19
    [3] => 41

    secont iteration
    [0] => 17
    [1] => 18
    [2] => 19
    [3] => 41
    [4] => 29
    [5] => 30
    [6] => 27
    [7] => 31
    [8] => 33
    [9] => 26
    [10] => 37
    [11] => 39
    [12] => 35
    [13] => 38

    thirt iteration
    [0] => 17
    [1] => 18
    [2] => 19
    [3] => 41
    [4] => 29
    [5] => 30
    [6] => 27
    [7] => 31
    [8] => 33
    [9] => 26
    [10] => 37
    [11] => 39
    [12] => 35
    [13] => 38

    Thus 60, 42, 45 and 56,24,25 are absent in the original array, because they were added by old keys that were already filled
    In the final array, there are six terms less than needed. So they then lose the post every time the post is updated.

    You can read more about + = here https://www.php.net/manual/ru/language.operators.array.php

    Why you use += ? Is this mistake or not?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter isinica

    (@isinica)

    Instead of += i use array_merge and it works fine. Final array looks

    (
        [0] => 17
        [1] => 18
        [2] => 19
        [3] => 41
        [4] => 56
        [5] => 24
        [6] => 25
        [7] => 40
        [8] => 29
        [9] => 30
        [10] => 27
        [11] => 31
        [12] => 33
        [13] => 26
        [14] => 37
        [15] => 39
        [16] => 35
        [17] => 38
        [18] => 60
        [19] => 42
        [20] => 45
    )

    Example of code
    instead $ranked_ids += array_intersect($post_terms, $ranked_terms);
    I use:
    $intersect_array = array_intersect($post_terms, $ranked_terms);
    $ranked_ids = array_merge($ranked_ids, $intersect_array);

    Plugin Author Aurovrata Venet

    (@aurovrata)

    hello @isinica

    thank you for your analysis. That’s indeed a mistake, += was used previously when associative arrays were used and post IDs were set as keys.

    If you find other issues/improvements, please do a PR on the GitHub repo directly, it will be easier to inspect your changes and merge them into the code 🙂

    updated in v2.10.1

    Thread Starter isinica

    (@isinica)

    Thank you, for you support!
    I updated, but still it seems to me that it works with an error
    Instead of this line

    $ranked_ids = array_merge(array_intersect($post_terms, $ranked_terms));

    need

    $ranked_ids = array_merge($ranked_ids, array_intersect($post_terms, $ranked_terms));

    In your variant, the array is constantly overwritten and in the end only the terms of the latest taxonomy in the final array

    Plugin Author Aurovrata Venet

    (@aurovrata)

    oops, my bad. Updated in v2.10.2

    Thread Starter isinica

    (@isinica)

    Thank you for your support!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Ranked is broke after post update’ is closed to new replies.