WordPress.org

Ready to get started?Download WordPress

Forums

Sidebar loop of search results for a custom field. (4 posts)

  1. enderpal444
    Member
    Posted 2 years ago #

    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.

  2. vtxyzzy
    Member
    Posted 2 years ago #

    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.

  3. enderpal444
    Member
    Posted 2 years ago #

    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.

  4. vtxyzzy
    Member
    Posted 2 years ago #

    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;
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.