301 Redirects Result in 404 for WordPress Search Query Strings

Redirect 301 /search?q=avengers https://www.example.com/?s=avengers You can’t match the query string with a mod_alias Redirect (or RedirectMatch) directive. The Redirect directive matches the URL-path only. You need to use mod_rewrite and match against the QUERY_STRING server variable. For example: RewriteCond %{QUERY_STRING} ^q=avengers$ RewriteRule ^search$ /?s=avengers [R=301,L] This will need to go before the existing WordPress front-controller. … Read more

WordPress get permalink function

From https://developer.wordpress.org/reference/functions/get_permalink/ : get_permalink( int|WP_Post $post, bool $leavename = false ) Retrieves the full permalink for the current post or post ID. […] Note that when used outside The Loop on a posts page (index, archive, etc.) without the ID parameter, it will return the URL of the last post in The Loop, not the … Read more

Create unique alphanumeric ID on wp_insert_post

See here the working version // function to save the randomString as custom field value function generate_AlphanumericID( $post_id ) { $postTypes = array(‘profile’, ‘article’); $postType = get_post_type( $post_id ); if (in_array( $postType, $postTypes ) ) { $characters = “0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ-_~’!,”; $charactersLength = strlen($characters); $randomString = ”; for ($i = 0; $i < 11; $i++) { $randomString … Read more

force category base – archive pages work WITH & WITHOUT category base slug in url?

I found a solution here: 404 on category.php pagination So, to fix my issue I just needed to add these custom rewrite rules to functions.php: add_action( ‘init’, ‘wpa58471_category_base’ ); function wpa58471_category_base() { add_rewrite_rule( ‘blog/([^/]+)/page/(\d+)/?$’, ‘index.php?category_name=$matches[1]&paged=$matches[2]’, ‘top’ ); add_rewrite_rule( ‘blog/([^/]+)/(feed|rdf|rss|rss2|atom)/?$’, ‘index.php?category_name=$matches[1]&feed=$matches[2]’, ‘top’ ); } Now, all blog pages work, including main blog page, archives, categories, and … Read more