Skip to content

Instantly share code, notes, and snippets.

@jazzsequence
Last active August 29, 2015 14:04
Show Gist options
  • Save jazzsequence/bc189e0469a290feca62 to your computer and use it in GitHub Desktop.
Save jazzsequence/bc189e0469a290feca62 to your computer and use it in GitHub Desktop.
search results that include posts2posts connections
<?php
$do_not_duplicate = array() // initialize the duplicates array. this will keep us from having duplicate posts in the results
/**
* set up an array of arguments to determine post2post relationships
* this stuff gets set up too late to be used in pre_get_posts so
* I have to use query_posts.
*/
$query_args = array(
'connected_type' => 'songs_to_artists',
'connected_items' => curated_posts_like_title( get_search_query() ),
// make sure this post isn't a duplicate
'post__not_in' => $do_not_duplicate
);
query_posts( $query_args );
// start the loop
if ( have_posts() ) : while ( have_posts() ) : the_post();
// add this post to the duplicate array so we don't get dupes
$do_not_duplicate[] = $post->ID;
// do normal WordPress loop stuff
wp_reset_query();
endwhile; endif;
<?php
/**
* Gets WordPress post ID when a title is LIKE something
*/
function curated_posts_like_title( $title = '' ) {
global $wpdb;
$results = null;
$posts = array();
if ( '' != $title ) {
$results = $wpdb->get_results( "SELECT ID FROM $wpdb->posts WHERE post_title LIKE '%" . $title . "%' AND post_status = 'publish'" );
foreach( $results as $result ) {
$posts[] = absint( $result->ID );
}
return $posts;
} else {
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment