Site logo

Archived topic

Related Posts Using Custom Fields and WP Show Posts

7 replies · Started by Brandon on April 8, 2022

Viewing posts 1–8 of 8

I'm currently using the following code in Elements with WP Show Posts. However, the logic isn't exactly what I want. What I'm trying to do here is make it so A) related posts are displayed based on a custom field in WordPress called 'similar_stuff' and B) if the 'similar_stuff' custom field doesn't exist in a post or is blank because not populated then, it displays related posts pulled from the same category as the post. The code below is using the tags. My objective is to avoid tags and use a custom field instead to map these posts. Any help is greatly appreciated! Thanks in advance! Full disclosure I'm not a coder so someone can show me what the updated code should look like that would be awesome.

<div class="wpsp-related-posts  grid-container">
    <strong><p>Similar Stuff:</p></strong>
    <?php
    if ( is_single() ) {
        $tags =  get_the_tags();
        $tags_list = [];
        foreach ($tags as $tag)
            $tags_list[] = $tag->slug;
        $tag_string = implode( ', ', $tags_list);
    } else {
        $tag_string = get_tag( get_query_var( 'tag' ) );
    }

    if ( ! $tag_string ) {
        $cats =  get_the_category();
        $cat = $cats[0];
        $tag_string = $cat->slug;
    }

    $list = get_page_by_title( 'related', 'OBJECT', 'wp_show_posts' );
    wpsp_display( $list->ID, 'tax_term="' . $tag_string . '"' );
    ?>
</div>

Hi there,

just to be clear - the custom field - I assume this will have different values for different 'similar_stuff' types?

Hi David,
Yes. My strategy is to apply different values to 'similar_stuff' for example:

similar_stuff = apples
similar_stuff = oranges
etc.

Hmmm... would you consider using a Custom Taxonomy over a custom field?
You can use this code to register a custom taxonomy:

// Generate custom taxonomy for Element Display Rules
function db_custom_tax() {
    register_taxonomy(
        'your-custom-taxonomy',
        array( 'post' ),
        array(
            'label' => __( 'Your Custom Taxonomy label' ),
            'rewrite' => array( 'slug' => 'your-custom-taxonomy' ),
            'hierarchical' => true,
            'show_in_rest' => true,
        )
    );
}

add_action( 'init', 'dbl_custom_tax' );

// Exclude custom taxonomy from wp_sitemaps
add_filter('wp_sitemaps_taxonomies', function( $taxonomies ) {
      unset( $taxonomies['your-custom-taxonomy'] );
      return $taxonomies;
  }
);

Note the your-custom-taxonomy strings in the code ( and the label which is what is displayed in the admin menu ), you need to change these for your custom tax slug.

Then you could do something like this:

<div class="wpsp-related-posts1  grid-container">
    <h2>Related Posts</h2>
    <?php
    $tax = 'your-custom-taxonomy';
    $tax_term = '';

    if ( is_single() ) {
        $cats =  get_the_terms( get_the_ID(), $tax );
        $tax_term = $cats[0]->slug;

        $args = array(
            'numberposts' => 3,
            'category_name' => $tax_term,
            'post__not_in' => array( get_the_ID() )
        );

        $posts = get_posts( $args );

        if ( ! empty( $posts ) ) {
            $tax_term = $tax_term;
        } else {
            $tags =  get_the_tags();
            $tax_term = $tags[0]->slug;
            $tax = 'post_tag';
        }
    }

    $list = get_page_by_title( 'related', 'OBJECT', 'wp_show_posts' );
    wpsp_display( $list->ID, 'taxonomy=' . $tax . '&tax_term=' . $tax_term );
    ?>
</div>

make sure to change the your-custom-taxonomy to your custom tax slug.

I would prefer to use the custom field because I know it's easy to update/apply in the posts. My end goal is to create some similarities while avoiding the need to create a bunch of tag pages that eat up crawl budget. Is there a way to use custom fields?

I am 99% positive it can be down with Custom Fields, its just not something I have in my snippet bin.
My reason for suggesting a custom tax is its more suitable for making very simple relationships and it has the added benefit of a WP admin list view so you can easily manage all posts for a specific term.

And the code I provided above, a) doesn't register an archive for the custom tax and b) excludes the tax from site maps. So to the best if my knowledge that leaves nothing to be crawled.

Oh, in that case! I'll give this a try. I'm not a developer and not familiar custom tax which is why I was hesitant but I'll give this a shot and research some. :)

Good luck !

This archived topic is closed to new replies.