Viewing 15 replies - 1 through 15 (of 56 total)
  • Hi @erikalleman, I’m not 100% sure about your question, but one idea that might be useful is that you can update a post in WordPress with the following php:

    
    // Update post with id '37'
    // @see https://developer.wordpress.org/reference/functions/wp_update_post/
    wp_update_post( array( 'ID' => 37 ) );
    

    which would update the ‘modified’ timestamp of the post.

    Thread Starter berry metal

    (@erikalleman)

    Hi Zack,

    I think that you have put me on the right track.

    This code

    add_action('wp_insert_comment','update_post_time',99,2);
    function update_post_time($comment_id, $comment_object) {
        // Get the post's ID
        $post_id = $comment_object->comment_post_ID;
        // Double check for post's ID, since this value is mandatory in wp_update_post()
        if ($post_id) {
            // Get the current time
            $time = current_time('mysql');
            // Form an array of data to be updated
            $post_data = array(
                'ID'           => $post_id, 
                'post_modified'   => $time, 
                'post_modified_gmt' =>  get_gmt_from_date( $time )
            );
            // Update the post
            wp_update_post( $post_data );
        }
    }

    will update the “last updated” date every time a comment is posted for that post.

    I need to modify the code so, that it will update the “last modified” date of that “post” post, that has the same taxonomy term (common taxonomy) as the “item” post type post, when it gets posted.

    Fictive Example:

    There is a “post” post called Fruits. (default post type)
    And there is an “item” post called Apple. (“items” post type)

    When Apple is getting posted, Fruits will fetch it automatically within a grid (via matching a common term of a common taxonomy of both posts).

    And what I need is that: after Apple got posted, the “last updated” date of Fruits will get updated.

    Could you give an example how to modify this code to achieve that?

    • This reply was modified 3 years, 1 month ago by berry metal.
    Moderator bcworkz

    (@bcworkz)

    So “Apples” is new post of an “items” post type and when any “items” post is newly posted you want a post named “Fruits” to have its last modified date updated to match? Or not so much “Fruits” as any post post_type with the same taxonomy term as the newly posted “items” post?

    Only newly published “items” posts? Or any modification of any “items” post? You need to hook the right action. Generally “draft_to_publish” for new posts (verify post type for this one). “publish_items” for any modification of an “items” post.

    In the callback query for any post where you want its modified date updated (with get_posts() ). With the correct post ID, modify as shown in an earlier example.

    Thread Starter berry metal

    (@erikalleman)

    So “Apples” is new post of an “items” post type and when any “items” post is newly posted you want a post named “Fruits” to have its last modified date updated to match?

    1. When any “items” post is newly posted, the term of that post need to be retrieved from X taxonomy.

    2. Then a “post” post need to be found, that has the same term in the same X taxonomy (known, existing taxonomy).

    3. The “last updated” date of that “post” post need to be updated to the post date of the corresponding “items” post.

    Or not so much “Fruits” as any post post_type with the same taxonomy term as the newly posted “items” post?

    Only “post” post types “last updated” date need to be updated. (the default post type).

    Only newly published “items” posts? Or any modification of any “items” post?

    Modifications (or deleting) of “items” posts do not need to trigger any date update.

    The whole code for this purpose should be just a function?

    Could you give an example code?

    I have written it in english instead of full php, could you help me to put it 100% php?

    add_action('what_here?','update_post_time',99,2);
    function update_post_time($items_id, $X_taxonomy) {
        // Get the post's ID
        $post_id = $newly_posted__items_post_ID;
        if ($post_id) {
            // Get the current time
            $time = current_time('mysql');
            // Form an array of data to be updated
            $post_data = array(
                'ID'           => $items_post_id, 
                'post_modified'   => $time, 
                'post_modified_gmt' =>  get_gmt_from_date( $time )
            );
            // Update the post
            wp_update_post_post( $post_data );
        }
    }
    Moderator bcworkz

    (@bcworkz)

    There’s a technical problem with your proposed scheme. With the block editor, taxonomy term assignments are performed in a separate process via the REST API. At the time any new save item post action fires, the assigned term is not yet available in the DB, as it is still in the process of being written, a relatively slow process. The term is also not available in the $_POST data that’s part of the item post save process.

    You could maybe hook an API action where the assigned term is available. I’m not sure if it’s all that easy to discern if the API action is due to an update or if it’s a new post. The inner workings of the API aren’t too familiar to me.

    Another possibility to accomplish your goal would be to create a scheduled task to run a few minutes after the save item post action fires that would update the related post. The related data will all be available by then. The task could query for the latest item post to determine the applicable term and date. The problem then would be if there are multiple item posts in a short period, the tasks wouldn’t know which item they should be using.

    Thread Starter berry metal

    (@erikalleman)

    The scheduled task should be done with WP CRON?
    I’m investigating that and I’ll be back.

    Thread Starter berry metal

    (@erikalleman)

    With the block editor, taxonomy term assignments are performed in a separate process via the REST API.

    But I don’t use the block editor. Did you mean Gutenberg?
    I don’t use any “builder”.

    The only thing I use is the basic text editor for the items post type posts.

    Because my “post” posts consist of “items” posts fetched together by relations by a dynamic query in a grid.

    • This reply was modified 3 years, 1 month ago by berry metal.
    Moderator bcworkz

    (@bcworkz)

    Yeah, WP Cron would be simplest, though server cron jobs can be made to work too.

    The block editor Gutenberg uses the API for itself. If you are using the classic editor, you might be able to pull the assigned term ID out of $_POST. Use your browser’s network developer tool to capture information from a POST request resulting from publishing a new item. If the assigned term ID is there, then it’ll be in $_POST as well.

    You can use the “save_items” action if “items” really is the CPT name. You’d then use get_posts() to get the latest post with the same term ID. With the right post ID, the remainder of your code is basically correct.

    Thread Starter berry metal

    (@erikalleman)

    I couldn’t find the data I was looking for, the term ID. Should I look in the response or in the header? However I didn’t see it anywhere.

    Could you give an example code that I could tweak further? (I mean just replace strings eventually if needed)

    But I need the correct architecture of the code based on the code above.
    Then I can find out what to replace with what, perhaps…
    I don’t know what to write in the add action parenthesis..
    I was looking for something like IFTTT for WordPress but I couldn’t find any yet.
    Thanks in advance!

    • This reply was modified 3 years, 1 month ago by berry metal.
    • This reply was modified 3 years, 1 month ago by berry metal.
    Thread Starter berry metal

    (@erikalleman)

    This is how it looks at the request tab after posting an items post called “test”, that was assigned to a term from a taxonomy called “relations”:

    flameshot_screenshot

    • This reply was modified 3 years, 1 month ago by berry metal.
    • This reply was modified 3 years, 1 month ago by berry metal.
    Thread Starter berry metal

    (@erikalleman)

    I have re-written the code:
    This is where I need help:
    $post_id = $”post” post ID that same term as newly posted “items” post;

    Something lik this?

    get the items post ID:
    $post_id = get_posts( $save_items )

    Match the terms:
    $post_term => $item_term

    add_action('save_items','update_post_time',99,2);
    function update_post_time($items_id, $items) {
        // Get the post's ID
        $post_id = get_posts( $save_items )
        if ($post_id) {
            // Get the current time
            $time = current_time('mysql');
            // Form an array of data to be updated
            $post_data = array(
                'ID'           => $post_id, 
                'post_modified'   => $time, 
                'post_modified_gmt' =>  get_gmt_from_date( $time )
    $post_term => $item_term
            );
            // Update the post
            wp_update_post( $post_data );
        }
    }

    But the post term and the item term both need to be saved in a variable first, and then match the 2 values with =>, is that right?
    I don’t know how to retrieve the terms based on the ID.

    Thanks!

    • This reply was modified 3 years, 1 month ago by berry metal.
    Thread Starter berry metal

    (@erikalleman)

    And I checked and in the response tab there is nothing, it’s empty. In the network tools.

    Thread Starter berry metal

    (@erikalleman)

    And I could add this perhaps to check if post type is “items”?
    if (array_key_exists('post_type', $_POST) && $_POST['post_type'] === 'items')

    Thread Starter berry metal

    (@erikalleman)

    I tried to write it again:

    add_action('save_items','update_post_time',99,2);
    function update_post_time($item_id, $item_object) {
        // Get the post's ID
        $item_id = $item_object->item_id;
        // Double check for post's ID, since this value is mandatory in wp_update_post()
        if ($item_id),
        if (array_key_exists('post_type', $_POST) && $_POST['post_type'] === 'items') {
            // Get the current time
            $time = current_time('mysql');
            // Form an array of data to be updated
            $latest_cpt = get_posts("post_type=items&numberposts=1");
            $latest_cpt[0]->ID
            $post_data = array(
                'post_type' => 'items',
    
                'tax_query' => array(
        array(
          'taxonomy' => 'relations',
          'field' => 'term_id',
                'ID'           => $item_id, 
                'post_modified'   => $time, 
                'post_modified_gmt' =>  get_gmt_from_date( $time )
    
            );
        $terms = get_the_terms( $post->ID , 'items' ); or
     $terms = get_terms_id_by_post_type( array($tax), array('items') );
              if ( $terms != null ){
                foreach( $terms as $term ) {
                $term->term_id    
    
                <<< and how do I match the terms here? >>>
    
            // Update the post
            wp_update_post( $post_data );
        }
    }
    Thread Starter berry metal

    (@erikalleman)

    I didn’t know how to refer to the fact that the post date that need to be updated is of the “post” post type, and how to match the terms of the 2 post types?

Viewing 15 replies - 1 through 15 (of 56 total)
  • The topic ‘How do I auto-update post date when new post type is fetched within the post?’ is closed to new replies.