admin管理员组

文章数量:1122846

I am not sure whether this is possible with the current architecture of WordPress, but I think there should be a way.

I have a WordPress site with around 11,000 posts and have recently added about 60 tags. Manually assigning these tags to each post is not feasible.

I want to display all posts on a tag archive page where the post titles contain the tag name or a value from a custom field (term meta) called "similar_names." This custom field (term meta)includes a few comma-separated words. If any word from the "similar_names" field is in the post title, it should be displayed when the tag page is viewed.

For example, if the tag is "Image SEO" and the "similar_names" field includes "image optimization, SEO images," I want the "Image SEO" tag page to list all posts that have "image optimization," or "SEO images" in their titles.

I tried using the pre_get_posts hook, but I was unable to complete the code even with the help of AI tools like ChatGPT.

I am not sure whether this is possible with the current architecture of WordPress, but I think there should be a way.

I have a WordPress site with around 11,000 posts and have recently added about 60 tags. Manually assigning these tags to each post is not feasible.

I want to display all posts on a tag archive page where the post titles contain the tag name or a value from a custom field (term meta) called "similar_names." This custom field (term meta)includes a few comma-separated words. If any word from the "similar_names" field is in the post title, it should be displayed when the tag page is viewed.

For example, if the tag is "Image SEO" and the "similar_names" field includes "image optimization, SEO images," I want the "Image SEO" tag page to list all posts that have "image optimization," or "SEO images" in their titles.

I tried using the pre_get_posts hook, but I was unable to complete the code even with the help of AI tools like ChatGPT.

Share Improve this question edited Jun 12, 2024 at 2:43 Ranuka asked Jun 12, 2024 at 2:38 RanukaRanuka 1,7942 gold badges18 silver badges31 bronze badges 3
  • Rather than writing code to circumvent the way the tag archive page operates, why not write code to automatically assign the tags to your posts? For example, you could attach a function to the save_post hook, examine the post title and assign the tags as you see fit. Then you can bulk edit and save all your posts, maybe 1000 at a time. – Dave S Commented Jun 12, 2024 at 2:54
  • @DaveS I thought about it, and let's say I created a new tag. So, I need to run code for all 11,000 posts; otherwise, the new tags will be added only to new posts. – Ranuka Commented Jun 12, 2024 at 15:25
  • OK, but you could also attach handlers for the appropriate taxonomy hooks like saved_{$taxonomy}. So every time you add a term, you walk through the posts and update them. Tags are used in lots of places besides the tag archive page. I guess the question is, do you want the terms to be assigned correctly to the posts, or do you want just a page to show the posts whose title contains a certain string. If the latter then why does that page have to be the tag archive? You could create a separate page to do it. – Dave S Commented Jun 12, 2024 at 20:20
Add a comment  | 

1 Answer 1

Reset to default 0

Use this-

function custom_tag_archive_query( $query ) {
    if ( is_tag() && $query->is_main_query() && !is_admin() ) {
        $tag = get_queried_object();
        $tag_name = $tag->name;
        $similar_names = get_term_meta( $tag->term_id, 'similar_names', true );

        $similar_names_array = explode( ',', $similar_names );
        $similar_names_array = array_map( 'trim', $similar_names_array );
        $similar_names_array[] = $tag_name; // include the tag name itself in the search terms

        $meta_query = array(
            'relation' => 'OR',
        );

        foreach ( $similar_names_array as $name ) {
            $meta_query[] = array(
                'key'     => 'post_title',
                'value'   => $name,
                'compare' => 'LIKE'
            );
        }

        $query->set( 'meta_query', $meta_query );
    }
}

add_action( 'pre_get_posts', 'custom_tag_archive_query' );

Ensure that the custom field similar_names is set up properly for your tags in the term meta. This code will modify the main query on tag archive pages to include posts whose titles match any word from the similar_names field or the tag name itself.

本文标签: How to Display Posts on a Tag Archive Page Based on Custom Field(term meta) Values in WordPress