Algorithms, Blockchain and Cloud

How to Use WordPress Search to Display Relevant Posts?


How to Create a Lightweight Related Posts Shortcode in WordPress
Display Relevant Posts in WordPress Without a Plugin
A Simple WordPress Shortcode to Show Related Posts
Build Your Own show_posts Shortcode in WordPress
Show Relevant WordPress Posts Using WP_Query
WordPress Shortcode for Related Posts with Optional Excerpts
Add Related Posts to WordPress Posts Without Extra Plugins
Lightweight Related Posts in WordPress Using Built-in Search
How to Use WordPress Search to Display Relevant Posts
Custom WordPress Shortcode: Show Matching Posts by Keyword
Wordpress Short Code Function: Showing Relevant Posts

A lightweight WordPress shortcode for displaying relevant posts using the built-in search query. Supports custom keywords, post count limits, and optional inline excerpts without needing an extra plugin.

In WordPress, you can create custom shortcode functions by adding code to your theme’s functions.php file, preferably in a child theme.

wordpress-pad

The following example adds a shortcode called show_posts, which can be used inside your posts to display a list of relevant posts. The relevant posts are retrieved using WordPress’s built-in search mechanism, similar to the /?s=keyword query parameter. You can also specify the number of posts to show and choose whether to display each post’s excerpt inline.

Compared with installing another WordPress plugin, a PHP shortcode like this is lightweight and gives you full control over the code. It can also reduce unnecessary overhead, as long as the implementation is kept simple and secure.

Also, with shortcode, you can choose where to place/show the relevant posts where the plugins only allow you to place them after the posts.

To use it, copy and paste the code below into your functions.php file. Then, inside any post or page, you can use the show_posts shortcode to insert the relevant post list.

For example:

[ show_posts keyword="wordpress" number="8" show_excerpt="true" ]

Will produce:

[ show_posts keyword="php" number="5" show_excerpt="false" ]

Will produce:

By default, number is set to 8 posts, and the posts show_excerpt is set to hide.

function show___posts_func($atts) {
    $atts = shortcode_atts(
        array(
            'keyword'      => '',
            'number'       => 5,
            'show_excerpt' => 'true',
        ),
        $atts,
        'show_posts'
    );

    $keyword = sanitize_text_field($atts['keyword']);
    $number = max(1, min(20, intval($atts['number'])));
    $show_excerpt = filter_var($atts['show_excerpt'], FILTER_VALIDATE_BOOLEAN);

    if ($keyword === '') {
        return '';
    }

    $query = new WP_Query(array(
        'post_type'           => 'post',
        'post_status'         => 'publish',
        's'                   => $keyword,
        'posts_per_page'      => $number,
        'ignore_sticky_posts' => true,
        'no_found_rows'       => true,
    ));

    if (!$query->have_posts()) {
        return '';
    }

    $output = '<ul class="show-posts-list">';

    while ($query->have_posts()) {
        $query->the_post();

        $output .= '<li>';
        $output .= '<a href="' . esc_url(get_permalink()) . '">';
        $output .= esc_html(get_the_title());
        $output .= '</a>';

        if ($show_excerpt) {
            $excerpt = get_the_excerpt();

            if ($excerpt) {
                $output .= '<br><small>' . esc_html(wp_trim_words($excerpt, 20)) . '</small>';
            }
        }

        $output .= '</li>';
    }

    $output .= '</ul>';

    wp_reset_postdata();

    return $output;
}

add_shortcode('show_posts', 'show___posts_func');

Wordpress is King!

–EOF (The Ultimate Computing & Technology Blog) —

707 words
Last Post: Microsoft Edge Settings That May Be Slowing Down Your Windows PC

The Permanent URL is: How to Use WordPress Search to Display Relevant Posts? (AMP Version)

Exit mobile version