Support » Fixing WordPress » How to create a list of links to pages that link to the current post? Plugin?

  • Hi all,

    I’m using a plugin which creates a product taxonomy, and displays a list of products on the site. What I’d really like to do is have a list of links on each product page that shows all the posts that reference that product. So basically, for taxonomy x, show all posts of taxonomy y that have a link to that post. The idea is that for any given product, people can go and read about that product in the posts that link to it.

    Is such a thing possible? Any plugin suggestions? I’ve tried searching for a plugin, but the search terms are pretty ambiguous (e.g., “plugin that shows a list of posts that link to the current page” doesn’t find many results ;)).

    I’m investigating something like this, https://wordpress.org/plugins/advanced-post-list/screenshots/ but not sure if it’ll do what I need.

    Any suggestions gratefully accepted!

Viewing 9 replies - 1 through 9 (of 9 total)
  • Let me know if I’m not undestanding this correctly but:

    You have:

    1. Post type for products
    2. Regular posts on your site that contain links to products on your site

    You would like:

    The product pages to automatically include links to the posts that link to that specific product.

    Possible solutions:

    1. Use the posts-to-posts plugin to create relationships between the posts and the products manually. Edit the product page template to query the related posts. https://wordpress.org/plugins/posts-to-posts/

    2. Enable pingbacks for the product post type and trigger the pingback request for all your posts. The comments for the products would then include the post links automatically as pingbacks.

    3. Just do it manually and add the links to the posts to each product as a custom field or something. 🙂

    Thanks, the posts-to-posts plugin looks like it’ll give me exactly what I need. LOL @ #3. 😀

    Quick update to this, for people who might be interested, I fixed it by knocking up my own plugin which implements this filter:

    function plant_articles_filter( $content ) {
        global $post;
        if( 'plant_lists' == $post->post_type && ! is_search() && is_single() )
        {
                // stuff that loads when the shortcode is called goes here
                $current_title = get_the_title();
                $currentID = get_the_ID();
    
                $search_term = html_entity_decode($current_title, ENT_QUOTES);
    
                $search_one = new WP_Query(array(
                            'order'          => 'ASC',
                            'orderby' 		 => 'title',
                            's'      => $search_term,
                            'post_status'    => null,
                            'nopaging' 	=> 1,
                            'post__not_in' => array($currentID),
                            'posts_per_page' => -10));
    
                $search_term = preg_replace("/[^A-Za-z ]/", '', $current_title);
    
                $search_two = new WP_Query(array(
                            'order'          => 'ASC',
                            'orderby' 		 => 'title',
                            's'      => $search_term,
                            'post_status'    => null,
                            'nopaging' 	=> 1,
                            'post__not_in' => array($currentID),
                            'posts_per_page' => -10));
    
                $search_three = new WP_Query(array(
                            'order'          => 'ASC',
                            'orderby' 		 => 'title',
                            's'      => $current_title,
                            'post_status'    => null,
                            'nopaging' 	=> 1,
                            'post__not_in' => array($currentID),
                            'posts_per_page' => -10));
                
                $search_articles = new WP_Query();
                $unique = array_unique(array_merge( $search_one->posts, $search_two->posts, $search_three->posts ), SORT_REGULAR);
                $search_articles->posts = $unique;
                $search_articles->post_count = count( $unique );
                
                $title_search_results = '<h3>Articles that mention ' . $current_title . ':</h3><ul>';
                $posts_count = 0;
    
                while($search_articles->have_posts()): $search_articles->the_post();
                    if( get_permalink() != '' && get_the_title() != '' )
                    {
                        $posts_count++;
                        $title_search_results .= '<li><a href="' . get_permalink() . '">' . get_the_title(). '</a></li>';
                    }
                endwhile; 
                
                if( $posts_count == 0 )
                {
                    $title_search_results = '';
                }
                else
                {
                    $title_search_results .= '</ul>';
                }
    
                wp_reset_query();
                
                $content .= $title_search_results;
        }
        return $content;
    }
    add_filter( 'the_content', 'plant_articles_filter', 0 );

    Works a treat – giving a dynamic bulleted list of articles based on a search on the current article title.

    You can see an example here: http://www.pumpkinbeth.com/plants/lathyrus-odoratus-naomi-nazareth/

    Hope this helps somebody else in future. 🙂

    Can you please explain how to use?
    I put the above code in my function.php file, but at the moment I have no idea what to put in the template to call that function.

    Thanks,
    Sven

    You’ll need to modify this to suit your own needs, it won’t just work out of the box, as it relies on a custom taxonomy. So in this case, it’ll only display the list for articles of post type ‘plants_list’.

    If you’re not using custom taxonomies, the best way would be to turn this into a shortcode, and apply it then on any pages where you want this list to appear.

    Thanks for your respond.
    I don’t need it on a page, I want to put it in a sidebar.
    But I’m more a front-end-designer, I understand CSS and only a little bit of php etc. I will keep searching and trying.

    I found two plugins but with them I have to connect all the pages manually …

    For my solution, putting it in a sidebar won’t work – it has to be in a particular page, because it uses the current page title to automatically find all other posts that contain the current page title within them, and build the list. This won’t work on a page where there are a number of articles.

    You really need to consider how you want this to automatically generate the list of links. By using the title of the plant taxonomy pages, mine finds all references to plants with that name (e.g., on the page titled “Daffodil”, it will list all pages that contain the word “Daffodil”). Unless you can come up with a process like that, you’ll struggle to find one that does it automatically. It may be easier to find a plugin/shortcode that will (for example) list all pages that fall under a particular tag, or category, and approach the problem that way.

    I find a solution.
    The side bar is active only on pages with lyrics and I want to show all related posts with links to that lyrics to show up in the sidebar.
    My final solution is to build this query:

    <?php global $post; $post_slug=$post->post_name; ?>

    <?php
    $args = array( ‘s’ => $post_slug, ‘cat’ => 4 );

    // The Query
    $the_query = new WP_Query( $args );

    // The Loop
    if ($the_query->have_posts() ) {
    while ( $the_query->have_posts() ) : $the_query->the_post();
    get_template_part( ‘entry-summary’ );
    endwhile;
    } else {
    _e( ‘Sorry, nothing matched your search. Please try again.’, ‘blankslate’ );
    }
    /* Restore original Post Date */
    wp_reset_postdata();
    ?>

    On my local machine this works fine.

    Thanks and greetings.

    Hi Webreaper,

    Your solution for pumpkinbeth.com looks fantastic.
    However I have difficulties implementing it myself.
    Would you be able to help me?
    Please email me at bsmolyanov@gmail.com

    Thanks!

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘How to create a list of links to pages that link to the current post? Plugin?’ is closed to new replies.