You can use get_adjacent_post() to get the nexy/prev post by tag. I think this should work,
// get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy )
$next_post = get_adjacent_post( true, '', false, 'post_tag' );
$prev_post = get_adjacent_post( true, '', true, 'post_tag' );
if ( $prev_post instanceof WP_Post ) {
printf(
'<div class="alignleft"><a href="https://wordpress.stackexchange.com/questions/345428/%s">« %s</a></div>',
esc_url( get_permalink( $prev_post->ID ) ),
esc_html( $prev_post->post_title )
);
}
if ( $next_post instanceof WP_Post ) {
printf(
'<div class="alignright"><a href="https://wordpress.stackexchange.com/questions/345428/%s">%s «</a></div>',
esc_url( get_permalink( $next_post->ID ) ),
esc_html( $next_post->post_title )
);
}
Related Posts:
- Edit tag cloud widget number
- How to automatically apply woocommerce product title to all product images alt tags?
- remove tags from posts in php
- tag search using WP_Query
- show limited tags in an article
- display most popular tags in two columns
- How to add a post or page tag to the list of classes appearing in the body tag’s class attribute?
- Displaying Only Certain Tags in Loop
- query if on page/2/?
- Highlight Current Tag in wp_tag_cloud
- Add relevant tag to search results
- Must Use Plugin Causing Query Error
- How to create a linked tag list in my sidebar
- Adding country tags automatically
- Way to querry data (tags) from a wordpress database?
- Adding Taxonomy Image to taxonomy loop [closed]
- If tag equals then show else show
- Way to bulk make all my tags lowercase?
- Pull Tags But Not as Links
- Adding Meta Tags to a Post using its Tags, Excerpt and content
- List posts that don’t have a specific tag?
- Edit the_category (); for a hierarchical display
- Need to put a script above tag in header.php – WP 5.7.1
- Use Tags to initiate Search
- Filtering a function’ output for a new continued function
- Trying to prepend a Hashtag symbol to the_tags links [closed]
- stripping tags from excerpt in WordPress is not working
- How to tag the main tag? [closed]
- Querying for specific tags
- Limit tags shown in post
- How to add Post Tags to a new element in header.php?
- Commas in Tag Cloud
- making my own “related pages” / “pages you might like” section
- Getting a WordPress tag to print inside of a traditional php echo
- remove specificly the last tag in all posts
- How to exclude labels from certain categories in a shortcode?
- Tag title not being returned in page title of tag archives
- How to add Text before my Custom Term and hide it when empty
- Multiple Tag Filtering
- clicking on the_tags() generated tags to show all tagged posts page
- How do I create a WP endpoint that retrieves all custom post types that have a tag?
- View tag description on page
- Display tag links on archive page?
- Using Tag Groups: Displaying groups and adjacent tags of current post
- Show content by using tags
- How to associate dynamic PHP page for chosen WordPress tag?
- Get a list of tags present in a paged front page
- How do you change the tag font size in the repeater-template.php [closed]
- PHP tag in post content makes wordpress go crazy
- Add Alt attribute to image served with php
- Notify admin (by email) if post added with specific tag
- Display product attributes for current product
- How to only fetch certain Tag ID’s
- A Space being added to url generated by custom plugin
- Insert wordpress tags below posts via functions.php
- Using tag to output text in Genesis?
- Is there a way to randomize and connect a background and header image?
- WordPress – show all posts + ones with specific tag
- Php echo into tag
- Change background image based on tags
- Exclude specific product tags from related products in WooCommerce 3+ [closed]
- how to loop through blog posts in php
- The search doesn’t work properly
- Cleaning up WordPress to improve performance?
- Network installation giving errors when viewing or adding a user
- Create a post variable processing page
- List all categories but exclude current post category on single post page
- Bare minimum to include in PHP file to use WP functions? [duplicate]
- Woocommerce, recognize the loop of related products
- WordPress different language footer text
- How do I target the child theme with get_bloginfo();?
- Can I use HTTP POSTs? Is there a better alternative?
- Getting a specific post values to another div or modal
- WP grandchild categories in nested ul li
- Modify HTML structure of fields in woocommerce checkout form
- How to improve my WordPress filter to include empty custom fields
- Update results, Before deleting the related category [closed]
- How to Return in WordPress Function php file?
- Limit Taxonomy Output in Conditional Statement
- Can’t retrieve element with WordPress default @fetch_rss();
- I have a snippet to redirect all users to a maintenance page. How do I exclude users with admin role?
- How to filter custom taxonomy term name, slug, and description?
- Is this a correct usage of ob_start() in my WordPress project?
- Searching Posts Programmatically in a WordPress Plugin
- Unique PHP on each Page
- WordPress Settings API unable to save settings
- Decode and Decrypt Azure B2C OpenID Authorization Token, Use Response in API Call (Example Token Within)
- Use WPQuery to match to specific repeater row in post
- Restrict wordpress access to logged users only
- Execute Shortcode in Textfile
- file_get_contents returning different in filters and actions
- How do I remove an action hook inside a class that is called by another class?
- Create page in templates for pages or the pages manager?
- wp_nav_menu prints children with parent name
- wp add inline style in loop
- Does wp_query and query_posts affect website performance? [duplicate]
- Facebook share button only share the_permalink(), without thumbnail, tilte, excerpt, etc
- Woocommerce – Shipping tax class based on cart items not using the highest tax available
- How to limit the number of posts in the wordpress loop in conjunction with the “download more” button?
- WordPress Query – Blog Cards Duplicate issue