Search
  • Sotware by
  • Christoph Amthor
Search Menu

Redirect URL to Post – Syntax

Note: This plugin is provided for free. Particularly if you use it to make money or in a profit-making context, however, I would appreciate a donation. Or check out my other plugins! Thank you!

Return to the main plugin page.

This plugin works invisibly in the background. When it is active, all URLs that contain certain keywords redirect to a post that is determined by these parameters. The result depends on the available posts at the time when the redirect happens. For example, the oldest post of the past month can be different today and tomorrow.

You can use the resulting URL as a link, in a menu or a button, or you can even send it around in a newsletter.

 

Actions

Add the query parameter redirect_to to the URL of your blog. This parameter is mandatory.

Values can be:

  • last or latest – The URL will redirect1 to the latest post.
  • first or oldest – The URL will redirect to the first (oldest) post.
  • random – The URL will redirect to a random post.
  • custom – The post will be determined according to the mandatory parameter orderby and the optional parameter order. Possible values for orderby are listed here. By default, order is “DESC” (descending – starting from the highest value, the later time or the bottom position in alphabetic order). Alternatively you can set it to “ASC” (ascending).

Obviously, the parameter “custom” lets you achieve the same as “latest”, “first” and “random”. These shortcuts exist just for convenience and readability.

Examples

Redirect to the latest post:

http://www.example.com/?redirect_to=latest

 

Redirect to the post with the highest number of comments:

http://www.example.com/?redirect_to=custom&orderby=comment_count

(Note: Replace everywhere “www.example.com” with your blog’s URL.)

 

Search Parameters

You can combine these actions with other search parameters in order to limit the considered posts to a certain subsample. Time parameters refer to the time of publishing. Supported are (according to superficial testing and the documentation):

authorauthor ID or comma-separated list of IDs
author_nameuses user_nicename (Try the last piece of the URL of the author’s page.)
catID or comma-separated list of IDs of categories; prepend a “-” to a number to exclude this category.
category_namecategory slug
date_query_beforeOnly posts from before the specified time will be considered.
The value can be anything that can be understood by strtotime(), including relative formats like “last monday” or “1 year ago”.2
date_query_afterSee date_query_before. This one restricts the choice of posts to those after the specified time. You can combine both parameters.
dayday of the month
excludecomma-separated list of post or page IDs
hour
minute
monthnummonth as number 1-12
post_typenote: requires has_archive for that post_type; default is post
ssearch for a keyword
second
tagtag slug
wweek of the year
has_password1 for posts with passwords; 0 for posts without passwords
tag_idtag id

Example

Redirect to a random post from the past month:

http://www.example.com/?redirect_to=random&date_query_after=1%20month%20ago

 

Other Parameters

If Nothing Was Found

You can also add a parameter default_redirect_to where you can specify a default destination if the other criteria return an empty result. It accepts as value the ID of a post or page. If this parameter is omitted or if the post or page with this ID cannot be found, then the default destination is the home page.

Tracking

Since version 0.5, you can also add parameters for tracking the redirects in your statistics. Supported are Google Analytics (utm_sourceutm_campaign and utm_medium) and Piwik Matomo (pk_campaign and pk_kwd).

Example

Redirect to the latest post that uses the tag with the ID 23 and add the campaign identifier “my_link” . If no post uses that tag, go to the post or page with the ID 12.

http://www.example.com/?redirect_to=latest&tag_id=23&default_redirect_to=12&utm_campaign=my_link

 

Shortcode to Create a Button

You can create a button in a page or text widget with a simple shortcode:

[redirect_to_post_button]

Possible parameters are:

div_class: Class name used for the enclosing div.
button_class:
Class name used for the button.
text: Button text. Put the text into quotes if it contains spaces. Default: “Random Post”
redirect_to: Sets the query parameter, see the explanations above. Default: “random”
params: Additional parameters. Formatted as: “key1=>value1,key2=>value2, …”

Example

[redirect_to_post_button text="Go to latest post about XY" redirect_to="latest" params="tag_id=>23"]

You can, of course, also create your own button, link or menu and just manually insert the URL.

 

FAQs and Troubleshooting

Please continue to this page.

Show 2 footnotes

  1. Technically the URL is rewritten, i.e. the browser address bar will show the new URL.
  2. You may have to write spaces as “%20”.
Share

2 Comments

    • I think we solved your issue already in the WordPress forums. Please always report there.

Comments are closed.