i have this custom search-result page

global $query_string;

$query_args = explode("&", $query_string);
$search_query = array(
            'nopaging'  => true,
            'post_type' => 'page'

foreach($query_args as $key => $string) {
    $query_split = explode("=", $string);
    $search_query[$query_split[0]] = urldecode($query_split[1]);
} // foreach

$search = new WP_Query($search_query);

<div class="page_title">
    <div class="container">

    <h1><?php echo $search->found_posts; ?> <?php if ( is_rtl() ) { echo 'نتائج بحث عن'; } else { echo 'Search Results Found For'; } ?> : "<?php the_search_query(); ?>"</h1>


<div class="content_fullwidth">

    <div class="container">

    <?php if ( $search->have_posts() ) : while ( $search->have_posts() ) : $search->the_post(); ?>

    <h1><a href="<?php echo get_permalink()?>"><?php the_title(); ?></a></h1>

    <?php endwhile; else : ?>
        <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
    <?php endif; ?>



the search results i am expecting will have custom fields for image and description , with the fields having different IDs depending on which page this result is from, i am having a problem figure out the correct code to pull out values from those custom fields.

<h1><a href="<?php echo get_permalink()?>"><?php the_title(); ?></a></h1>

working fine because it is more of a generic function that will work on all the results. so my problem is figuring out a dynamic code to pull out those custom fields values knowing that they have different IDs in terms of the page ID and the Custom field ID it self.

note: i am using CMB2 to display custom metaboxes , below is a sample of a metabox displayed on a specific page

function mobile_adv_metabox() {

// Start with an underscore to hide fields from custom fields list
$prefix = 'mobile_adv_';

 * Metabox to be displayed on a single page ID
$cmb_mobile_adv = new_cmb2_box( array(
    'id'           => $prefix . 'metabox',
    'title'        => __( 'Page Details', 'cmb2' ),
    'object_types' => array( 'page', ), // Post type
    'context'      => 'normal',
    'priority'     => 'high',
    'show_names'   => true, // Show field names on the left
    'show_on'      => array( 'id' => array( 10 ) ), // Specific post IDs to display this metabox
) );

$cmb_mobile_adv->add_field( array(
    'name'    => 'Service Image',
    'desc'    => 'Upload an image or add one from the library. ',
    'id'      => $prefix . 'service_image',
    'type'    => 'file',
    'options' => array(
        'url' => false,
) );

$cmb_mobile_adv->add_field( array(
    'name'    => 'Service Description',
    'desc'    => 'Add a description for the service.',
    'id'      => $prefix . 'service_desc',
    'type'    => 'textarea',
) );

You just need to get the data using get_post_meta().

<h1><a href="<?php echo get_permalink()?>"><?php the_title(); ?></a></h1>
<span class="description">
    <?php echo get_post_meta( get_the_ID(), 'mobile_adv_service_desc', true ); ?>

This Plugin stores your values as WordPress metadata. So to retrieve the values in the frontend, you can use the built in WordPress functions for that.

| improve this answer | |
  • i understand that i can use this function to get the metadata , but what is confusing me is the $key parameter in the get_post_meta() function , this parameter will not always be 'mobile_adv_service_desc', it will varies depending on the metabox $prefix , because i am expecting different pages in the result so this means different metabox $prefix – Mohamed Mokhtar Apr 29 '15 at 11:41
  • In this case you will have to make a switch, because you need to define which information you want to show. For example, switch for post_type. – fischi Apr 29 '15 at 12:46
  • i'm sorry i am not following you , can you explain more please ? , and on a side note i found out when i write the get_post_meta() function for all the metafields i have inside the loop after i do the search i get the metavalues displayed like i want , but the thing is i don't think this is dynamic enough for later on . i will have to modify this code if i add another pages and metaboxes later in the future. – Mohamed Mokhtar Apr 29 '15 at 13:12

Put this in your custom search page instead of your code, and don't forget to define your post types in the query arguments..

// Query arguments
global $wpdb;
// If you use a custom search form
// $keyword = sanitize_text_field( $_POST['keyword'] );
// If you use default WordPress search form
$keyword = get_search_query();
$keyword = '%' . like_escape( $keyword ) . '%'; // Thanks Manny Fleurmond
// Search in all custom fields
$post_ids_meta = $wpdb->get_col( $wpdb->prepare( "
    SELECT DISTINCT post_id FROM {$wpdb->postmeta}
    WHERE meta_value LIKE '%s'
", $keyword ) );
// Search in post_title and post_content
$post_ids_post = $wpdb->get_col( $wpdb->prepare( "
    SELECT DISTINCT ID FROM {$wpdb->posts}
    WHERE post_title LIKE '%s'
    OR post_content LIKE '%s'
", $keyword, $keyword ) );
$post_ids = array_merge( $post_ids_meta, $post_ids_post );
// Query arguments
$args = array(
    'post_type'   => array('post'),
    'post_status' => 'publish',
    'post__in'    => $post_ids,
$query = new WP_Query( $args );
if ( $query->have_posts() ): while ( $query->have_posts() ) : $query->the_post();
// Do loop here
    echo '<a href="'.get_permalink().'">';
    echo the_title();
    echo '</a>';
    echo '<br>';
    echo the_content();


else :
    _e('Sorry, no posts matched your criteria.');


Thank you. Best, EF.

| improve this answer | |
  • Why do you use SQL queries when WordPress has all this built in? Also, the answer is not connected to the question - it was about how to displaying the Metainformation. – fischi Apr 29 '15 at 11:15
  • It is easier, to get all data from DB... anyway my answer is helping to display the Metainformation, I don't know if you even try the code or not.... but what you are correct about, is that I didn't understand this question completely. Thank you for correct me. – efoula Apr 29 '15 at 15:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.