WordPress.org

Support

Support » How-To and Troubleshooting » Sidebar loop of search results for a custom field.

Sidebar loop of search results for a custom field.

  • On my posts pages I want to show the search results for a custom field associated with the post. How do I set that up. I looked around but couldn’t find an example.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The My Custom Widgets plugin will let you create a custom widget that can include PHP code to do what you want.

    Also, it is simple to add shortcode support to widgets. Then, you could code your own shortcode function which would be called from a text widget.

    I right my own loops that put in custom sidebars. What Im really looking for is to make a loop that returns the search results for a custom field that corresponds with the post. So if I have a custom field that is a name then I can make the search results equal to that name.

    I think what you want is something like this:

    <?php
    $search_term = get_post_meta($post->ID,'name',true);
    $args = array(
       'posts_per_page' => 5,
       's' => $search_term,
    );
    $my_search = new WP_Query($args);
    if ($my_search->have_posts()) : while ($my_search->have_posts()) : $my_search->the_post();
          echo '<h2>';the_title();echo '</h2>';
          the_content();
       endwhile;
    endif;
    ?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Sidebar loop of search results for a custom field.’ is closed to new replies.
Skip to toolbar