qlstudio

WP / get_post_by_meta()

Dec 23rd, 2014
155
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.          /**
  2.          * Get Post object by post_meta query
  3.          *
  4.          * @use     $post = get_post_by_meta( array( meta_key = 'page_name', 'meta_value = 'contact' ) )
  5.          * @since       1.0.4
  6.          * @return      Object      WP post object
  7.          */
  8.         function get_post_by_meta( $args = array() )
  9.         {
  10.            
  11.             // Parse incoming $args into an array and merge it with $defaults - caste to object ##
  12.             $args = ( object )wp_parse_args( $args );
  13.            
  14.             // grab page - polylang will take take or language selection ##
  15.             $args = array(
  16.                 'meta_query'        => array(
  17.                     array(
  18.                         'key'       => $args->meta_key,
  19.                         'value'     => $args->meta_value
  20.                     )
  21.                 ),
  22.                 'post_type'         => 'page',
  23.                 'posts_per_page'    => '1'
  24.             );
  25.            
  26.             // run query ##
  27.             $posts = get_posts( $args );
  28.            
  29.             // check results ##
  30.             if ( ! $posts || is_wp_error( $posts ) ) return false;
  31.            
  32.             // test it ##
  33.             #pr( $posts[0] );
  34.            
  35.             // kick back results ##
  36.             return $posts[0];
  37.            
  38.         }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×