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 |1 Answer
Reset to default 0Use 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
版权声明:本文标题:How to Display Posts on a Tag Archive Page Based on Custom Field(term meta) Values in WordPress? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736303703a1931976.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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:54saved_{$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