Rewrite custom post type URL in search - wordpress

I have a website in which I have a custom post type (guest authors from CoAuthors Plus). With a plugin I managed to make post of custom type "guest author" searchable by WordPress legacy search.
Now, the authors are correctly shown in search results. Although, they are linked to a wrong page, /?post_type=guest-author&p=2148, which brings to a 404.
I'd like to be able to get the URL, interprete it, and redirect to the correct page (which is in the form of /archives/author/name-surname/.
I'm trying to get it working with a rewrite URL, but I'm not able to catch the data and formulate the rewrite.

The following code changes the permalinks for guest-authors. It uses the methods from the CoAuthor plugin that output the guest authors link.
At least now you have the correct links according to the plugin's intentions.
They will be in the form:
{site_url}/author/{author_slug}
Here is the code to include in functions.php:
function adjust_permalink($permalink, $post){
$post_type = get_post_type($post);
if($post_type === 'guest-author'){
global $coauthors_plus;
$author = $coauthors_plus->get_coauthor_by('ID', $post->ID);
$permalink = get_author_posts_url( $author->ID, $author->user_nicename );
}
return $permalink;
}
add_filter('post_type_link','adjust_permalink',10,2);
Now, you should be able to create your template php file for author in your theme: author.php

Related

Show custom post archive when custom post not specified

I have a custom post type called produce set up in WordPress and a custom taxonomy called produce_category.
The URL format for posts in this custom post type is in this format http://mywebsite/produce/%produce_category%/%postname%/. Of course %produce_category% and %postname% get substituted accordingly, a working example url would be http://mywebsite/produce/fruits-and-vegetables/avocado.
What I would like to do is show the produce_category custom taxonomy archive if a user visits http://mywebsite/produce/%produce_category%, without specifying post name at the end, e.g http://mywebsite/produce/fruits-and-vegetables to show all produce in fruits-and-vegetables produce_category.
Besides that when a user visits http://mywebsite/produce/ I would like to show all the produce archive. EDIT: I have this working now.
I know how to create the archive pages totally fine and have no problem with that. I am stuck at creating permalinks. When I visit http://mywebsite/produce/%produce_category% I get a 404 error.
I'm looking for advise on the best way to implement this. Currently I am using Custom Post Type Permalinks and CPTUI.
The CPTUI custom taxonomy settings interface does not allow me to have a blank in the custom rewrite slug. It defaults to the custom taxonomy slug, produce_category, when I don't fill in anything.
This gives the front-side produce_category taxonomy archive url as http://mywebsite/produce/produce_category/%produce_category%/ e.g. http://mywebsite/produce/produce_category/fish-and-seafood/ when what I want for the archive is http://mywebsite/produce/fish-and-seafood/.
Please help with suggestion on the best way I can achieve the custom taxonomy url.
Thank you all.
Try this code. It will help you to achieve your url structure... Make sure you update permalinks after saving it to functions.php
function custom_produce_category_link( $link, $term, $taxonomy )
{
if ( $taxonomy !== 'produce_category' )
return $link;
return str_replace( 'produce_category/', '', $link );
}
add_filter( 'term_link', 'custom_produce_category_link', 10, 3 );

How would I structure this url functionality in wordpress

What I'm trying to do in Wordpress is import movie data from the movie database API, and display it inside a wordpress site. I'm just having trouble figuring out what would be the best way to set this up in the backend with a clean url structure.
I created a template file, and its going to have a GET variable with the movies title in it. I would want my domain to look like
mydomain.com/movie/TITLEGOESHERE
how could i set this up through wordpress, so the page I created works that way.
This would not be pulling post data from inside the wordpress. Would htaccess work here?
EDIT:
The way im thinking of it right now with a dirty url is, mydomain.com/pagewithAPIcode/?movie=MOVIETITLE, that way I can get the MOVIE TITLE as a GET variable, and then pass it to the api to grab the external movie info. And then with htaccess, rewrite it so it looks nicer
In the following code, a query variable mvtitle is added in which the movie title will be available. A rewrite rule is also created which for addresses matching the movie/{some-title} scheme displays the page movie (post type page). To read value of the mvtitle use get_query_var() function.
add_filter( 'query_vars', 'so59386348_query_vars' );
add_filter( 'page_rewrite_rules', 'so59386348_page_rewrites' );
function so59386348_query_vars( $vars )
{
$vars[] = "mvtitle";
return $vars;
}
function so59386348_page_rewrites( $rewrite_rules )
{
$page_slug = 'movie';
$rewrite_rules["$page_slug/([^/]+){1}/?$"] = 'index.php?pagename='.$page_slug.'&post_type=page&mvtitle=$matches[1]';
return $rewrite_rules;
}
$title = get_query_var( 'mvtitle', FALSE );
echo 'Movie title: ' . $title;
Please follow below instructions:
Log in to your WordPress website.
When you're logged in, you will be in your 'Dashboard'.
Click on 'Settings'.
On the left-hand side, you will see a menu. In that menu, click on 'Settings'.
Click on 'Permalinks'.
The 'Settings' menu will expand providing you additional options. Click on 'Permalinks'.
Select 'Post name'.
Click 'Save changes'.
I hope it would help you out.
Open dashboard
Search for settings
Click to permalinks in settings
Look for Permalinks common settings
in Custom structure use the url like %category%/%postname% and save
Enjoy!!
First of all open the admin panel, Go to settings->permalinks and in the custom settings, change the custom structure such as:
%custom_name%/%postname%
You can change the custom name with category or any other static name you want.

Redirect WordPress URLs to parent page without changing URL

I am having an issue with redirect rules for a single-page app that is on a sub-page of a Wordpress site. I have followed this set of instructions pretty directly and am still having issues: https://wordpress.stackexchange.com/questions/193479/redirect-sub-pages-to-parent-without-changing-url
The subpages are custom post types for business locations. When someone visits http://business.com/hollywood-ca/contact it should pull up http://business.com/hollywood-ca/ but the url needs to remain the same (The contact portion of the URL is part of a single-page Vue.js app on each location page, so it needs to stick around). Here is my code:
//This function hooks in on post save
function add_location_deep_links( $post_id, $post, $update ) {
$post_type = get_post_type($post_id);
if ( "location" != $post_type ) return; // If this isn't a 'location' post, don't update it.
$slug = $post->post_name; //hollywood-ca
add_rewrite_rule(
"^{$slug}/[A-Za-z]", //regex prevents trying to redirect the /hollywood-ca/ page
"index.php?page_id={$post_id}", //redirect to post
'top' // take precedence over other redirects
);
flush_rewrite_rules();
}
The problem is when I visit http://business.com/hollywood-ca/contact the page redirects to http://business.com/hollywood-ca/ which prevents my single-page app from navigating to the contact tab.
If it helps, I have also written a couple of functions that change my URLs from business.com/location/hollywood-ca to the cleaner business.com/hollywood-ca. I have tested these issues without those functions and am still having issues, so I don't think they are related.
I got an answer on Wordpress Exchange. I needed to query index.php with the custom query variable for my custom post type instead of page_id:
add_rewrite_rule(
"^{$slug}/",
"index.php?location={$slug}", //changed query var to location
'top'
);

How to map an arbitrary URL to a function in a Wordpress plugin

I'm trying to create a Wordpress plugin that redirects visitors of example.com/redirect/XXX to a different page based on the value of XXX. I think I know how to do the redirect logic, but I don't know how to make sure that my Wordpress plugin function will be called when a visitor goes to example.com/redirect. Right now I just get a 404. There are other solutions that involved changing the .htaccess file, but I want this to function as a standalone plugin. Thanks!
When I need this kind of things i just create a common page with template as the "plugin", a page with all the functions that I need.
For example, if i need a shopping cart, I just create a cart.php as:
<?php
/*
Template Name: Cart
*/
// functions here
?>
And I go to my wp-admin and create a page with Cart as its template.
Depending on what exactly you want to do which is quite vague ( when you say page you actually mean page ? post ? cpt ? and when you say plugin functions - what are they ? and do you use permalinks ?)
.. but under some conditions you could use wp conditionals .
example ( from codex )
is_singular( 'foo' )
// Returns true if the post_type is "foo". execute plugin hook
is_singular( array( 'foo', 'bar', 'baz' ) )
// Returns true if the post_type is "foo", "bar", or "baz".
// See also the Custom Post Types book.
or if you aim at filtering you can always hook to pre_get_post with is_main_query() or any other conditional //

Wordpress auto-generated "canonical" links - how to add a custom URL parameter?

Does anyone know how to modify the Wordpress canonical links to add a custom URL parameter?
I have a Wordpress site with a page that queries a separate (non-Wordpress) database. I passed the URL parameter "pubID" to display individual books and it is working OK.
Example: http://www.uglyducklingpresse.org/catalog/browse/item/?pubID=63
But the individual book pages are not showing up properly in Google - the ?pubID parameter is stripped out.
I think maybe this is because all the item pages have the same auto-generated "canonical" URL link tag in the source - one with the "pubID" parameter stripped out.
Example: link rel='canonical' href='http://www.uglyducklingpresse.org/catalog/browse/item/'
Is there a way to perhaps edit .htaccess to add a custom URL parameter to Wordpress, so that the parameter is not stripped out by permalinks and the "canonical" links?
Or maybe there's another solution ... Thank you for any ideas!
You should be able to replace Wordpress's rel_canonical action function with your own function in which (when your conditions are meet) you create a canonical link appending the query string variable. The following should work, although you'll probably need to change the conditions to meet your needs.
remove_action('wp_head', 'rel_canonical');
add_action('wp_head', 'my_rel_canonical');
function my_rel_canonical() {
if (is_page('item') && isset($_GET['pubID'])) {
global $post;
$link = get_permalink($post->ID) . '?pubID=' . absint($_GET['pubID']);
echo "<link rel='canonical' href='$link' />\n";
} else {
rel_canonical();
}
}

Resources