I need to overwrite the records from the custom type of records in the reverse order. I tried to add such code to the functions.php file.

$args = array( 'numberposts' => -1, 'post_type' => 'otcheti' ); $othecti = get_posts($args); $othecti_revers = array_reverse(get_posts($args)); for ($i = 0; $i < count($othecti); $i++) { $newOthect = $othecti_revers[$i]; $newOthect->ID = $othecti[$i]; wp_insert_post($newOthect); } 

Unfortunately, no rewriting takes place. Tell me, what could be the matter?

Maybe there is some easier way to overwrite posts in a custom post type in reverse order?

    2 answers 2

    Maybe there is some easier way to overwrite posts in a custom post type in reverse order?

    In the array $ args add the orederby parameter with the desired value of "inverse"

    Cm:
    https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters
    https://wp-kama.ru/function/wp_query#parametry-sortirovki-i-poryadka

    • I seem to be expanding an array using array_reverse. I have more problems with rewriting. - PC Tea
    • I read the link far and wide and I did not see the answer how to overwrite the records in the reverse order. A more detailed answer is possible, why is array_reverse not appropriate? And what is the meaning of 'reverse'? - PC Tea
    • Yes, what nafik array_reverse ! Do not rubbish garbage. get_posts IMMEDIATELY form what you need and how you need it. Parameters are the same as wp_query - SeVlad
    • And what with what forms? You read the question, it is written that you need to rewrite the records. I can post posts in any order anywhere, at least in order, at least backwards, somehow. I could not overwrite, and did not output in the reverse order. - PC Tea

    Understood.

    In general, it makes sense to use such a cycle and update only those fields that need to be updated, rather than trying to completely overwrite the object:

     for ($i= 0; $i < count($posts); $i++) { wp_update_post(array( 'ID' => $posts[$i]->ID, 'post_title' => $posts_reverse[$i]->post_title, 'post_content' => $posts_reverse[$i]->post_content, )); }