• Aimee

    (@greatdanemaniac)


    Hi!
    I’s like to know if there’s a way to combine preg_match and wp_insert_post_data to fetch the first sentence of an instagram post when it’s importing to your WordPress? Basically to have it match the first dot, exclamation mark(!) or question mark(?)

    Right now I have to choose to either set a default post title which get’s boring over time (and confusing, maybe?), or have the whole caption as the post title and no description in the post content or repeat all of the text again.

    I’m all ears for suggestions, and I’m an complete noob, so the simpler solution the better. Right now I’m using this, and it doesn’t work…

    function filter_post( $data , $postarr ) {
    
        if (!preg_match('/(\.|!|\?)\s/',
            $data['post_title']));
    
        return ( $data );
    }
    
    add_filter ( 'wp_insert_post_data' , 'filter_post' , '99', 2 );

    Any suggestions on how to solve this? BTW, found this code here and modified it a bit.

    Thanks!

Viewing 1 replies (of 1 total)
  • Thread Starter Aimee

    (@greatdanemaniac)

    Solved it, sort of anyway. Used another function instead; save_post and it works. However, i’d still like to make this work together with any preg_ function to fetch the first sentence of the post.

    add_action( 'save_post', 'save_post_wpse_87921', 10, 2 );
    
    function save_post_wpse_87921( $post_id, $post_object ){
        // Auto save?
        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
            return;
    
    		$category = array('photos', 'instagram');
    
    	if ( $post_id && !in_category($category, $post_id))
    		return;
    
        $new_title = wp_trim_words( $post_object->post_title, $num_words = 7, $more = '...' );
    
        // Unhook this function so it doesn't loop infinitely
        remove_action( 'save_post', 'save_post_wpse_87921' );
    
        // Call wp_update_post update, which calls save_post again.
        wp_update_post( array(
            'ID' => $post_id,
            'post_title' => $new_title,
    		'post_name' => ''
        ));
    	set_post_format($postID, 'image');
        add_action( 'save_post', 'save_post_wpse_87921', 10, 2 );
    }
Viewing 1 replies (of 1 total)
  • The topic ‘Get first sentence of instagram caption as post title?’ is closed to new replies.