wp_link_query

Definition:
function wp_link_query( $args = array() {}

Performs post queries for internal linking.

Parameters

  • array $args: Optional. Accepts ‘pagenum’ and ‘s’ (search) arguments.

Return values

returns:Results.

Source code

function wp_link_query( $args = array() ) {

	$pts = get_post_types( array( 'public' => true ), 'objects' );

	$pt_names = array_keys( $pts );



	$query = array(

		'post_type' => $pt_names,

		'suppress_filters' => true,

		'update_post_term_cache' => false,

		'update_post_meta_cache' => false,

		'post_status' => 'publish',

		'order' => 'DESC',

		'orderby' => 'post_date',

		'posts_per_page' => 20,

	);



	$args['pagenum'] = isset( $args['pagenum'] ) ? absint( $args['pagenum'] ) : 1;



	if ( isset( $args['s'] ) )

		$query['s'] = $args['s'];



	$query['offset'] = $args['pagenum'] > 1 ? $query['posts_per_page'] * ( $args['pagenum'] - 1 ) : 0;



	// Do main query.

	$get_posts = new WP_Query;

	$posts = $get_posts->query( $query );

	// Check if any posts were found.

	if ( ! $get_posts->post_count )

		return false;



	// Build results.

	$results = array();

	foreach ( $posts as $post ) {

		if ( 'post' == $post->post_type )

			$info = mysql2date( __( 'Y/m/d' ), $post->post_date );

		else

			$info = $pts[ $post->post_type ]->labels->singular_name;



		$results[] = array(

			'ID' => $post->ID,

			'title' => trim( esc_html( strip_tags( get_the_title( $post ) ) ) ),

			'permalink' => get_permalink( $post->ID ),

			'info' => $info,

		);

	}



	return $results;

}

10837

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: