Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Created April 20, 2017 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tommcfarlin/13ff8610716de3cb5b5e612d80267e9e to your computer and use it in GitHub Desktop.
Save tommcfarlin/13ff8610716de3cb5b5e612d80267e9e to your computer and use it in GitHub Desktop.
[WordPress] On Writing Clever Code with Arrays in WordPress
<?php
// Get the excerpt from the incoming post.
$post = get_post( $post_id );
$excerpt = $post->post_excerpt;
/**
* And we update the post content without the information (and we don't need
* paragraph tags).
*/
$event_post->post_excerpt =
apply_filters(
'the_excerpt',
implode( ', ',
array_diff(
array_map(
'trim',
explode( ',', $excerpt )
),
array( $name ) ),
),
);
<?php
// Get the excerpt from the incoming post.
$post = get_post( $post_id );
$excerpt = $post->post_excerpt;
// Remove the name from the array of names in the excerpt.
$to_remove = array( $name );
$names = array_map( 'trim', explode( ',', $excerpt ) );
$result = array_diff( $names, $to_remove );
// Now creae the new excerpt.
$new_excerpt = implode( ', ', $result );
/**
* And we update the post content without the information (and we don't need
* paragraph tags).
*/
$event_post->post_excerpt = apply_filters( 'the_excerpt', $new_excerpt );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment