So I'm trying to remove the canonical link in the header of WordPress for paginated pages but all the suggestions I tried aren't working.
Here is my code which is in my functions.php file:
function my_add_noindex_tags(){
$paged = intval( get_query_var( 'paged' ) );
if(is_search() || is_404() ) :
echo '<meta name="robots" content="noindex,follow">';
endif;
if ($paged >= 2) :
add_filter( 'wpseo_canonical', '__return_false', 10, 1 );
remove_action('wp_head', 'rel_canonical');
echo '<meta name="robots" content="noindex,follow">';
endif;
}
add_action('wp_head','my_add_noindex_tags', 4 );
I know the code inside if ($paged >= 2) : runs because this tag <meta name="robots" content="noindex,follow"> is in the head section.
Anyone know where I might be going wrong.
The issue here is the canonical link added by Yoast SEO aren't properly removed as expected.
Cheers
After going through the Yoast SEO codes, it seems like the canonical action is added to wpseo_head action.
You either have to use priority 1 when adding your function to run in wp_head to get this to execute properly, or do it with the appropriate method below ie. using wpseo_head action.
function remove_canonical() {
$paged = intval( get_query_var( 'paged' ) );
if ($paged >= 2) {
add_filter( 'wpseo_canonical', '__return_false', 10 );
}
}
add_action( 'wpseo_head', 'remove_canonical', 4);
For me only adding this to init action worked
add_action('init', function() {
add_filter( 'wpseo_canonical', '__return_false', 10 );
});
All these answers didn't help me and i couldn't find the right answer for my scenario:
I had to set priority to higher than 10 because apparently Yoast changes the url at priority 10 so if you want to change their url you have to set a higher priority:
Change the canonical url with a custom function:
add_filter( 'wpseo_canonical', 'change_canonical', 20 );
Delete the canonical url:
add_filter( 'wpseo_canonical', '__return_false', 20 );
Be aware that if you do not set a priority it will use priority 10 as the default and it will not work.
I know I'm late to the party, but for the right implementation, you need to run 'wpseo_canonical' filter directly:
function remove_canonical_pagination() {
$paged = intval( get_query_var( 'paged' ) );
if ($paged >= 2) {
return false;
}
}
add_action( 'wpseo_canonical', 'remove_canonical_pagination', 4);
Related
somebody tried to fix pagination problem on my site by adding a function to your functions.php file[![screenshot - console][1]][1]
unfortunately, after updating wordpress or acf, this function does not work and when you try to go to the next page in the "atom" category, it displays 404 - Sorry, this page does not exist.
The pagination problem concerns only one category (subcategory). In the functions.php file I found a function like this:
function fix_atom_category_paged_query( $q ) {
if ( ! is_admin() && is_category( 'atom' ) && $_GET['debug'] == 1 ) {
$q->set( 'post_type', 'post' );
$q->set( 'posts_per_page', 9 );
// wp_die( var_dump( $q ) );
return $q;
}
}
add_action( 'pre_get_posts', 'fix_atom_category_paged_query', 2, 1 ); ```
[1]: https://i.stack.imgur.com/rKqzT.png
I suspect that the problem with the category called "atom" is due to the canonical link building and the wordpress core build itself. Names such as rss, feed, rss2, rdf, atom may conflict.
Ok, I tested it on another site. If category is called "atom", then wordpress pagination does not work for the archive of this category. I think this is WordPress problem.
I'm using a snippet to overwrite the canonical URL in YoastSEO for WordPress/WooCommerce. The snippet is based on the official docs example: https://developer.yoast.com/features/seo-tags/canonical-urls/api/
Here's my code:
function prefix_filter_canonical_example( $canonical ) {
if (is_shop() && is_paged() ) :
$canonical = get_permalink(woocommerce_get_page_id( 'shop' )).'page/'.get_query_var('paged').'/';
elseif(WCV_Vendors::is_vendor_page()):
$vendor_shop = urldecode( get_query_var( 'vendor_shop' ) );
$vendor_id = WCV_Vendors::get_vendor_id( $vendor_shop );
$canonical = WCV_Vendors::get_vendor_shop_page( $vendor_id );
endif;
return $canonical;
}
add_filter( 'wpseo_canonical', 'prefix_filter_canonical_example' );
The code doesn't do anything to the canonical URL regardless of the content I return. But the if/else works fine and if I echo the content of $canonical I see the correct URLs.
I tried it already with the basic storefront theme and I deactivated nearly all plugins. But the snippet won't work. Is there anything I miss?
If you don't pass any priority to the action wordpress will take it as 10. Yoast SEO also call this filter to add canonical url so wordpress executed function in the order in which they were added to the action.
So change this line
add_filter( 'wpseo_canonical', 'prefix_filter_canonical_example' );
With this
add_filter( 'wpseo_canonical', 'prefix_filter_canonical_example', 20 );
I would like:
http://www.gadgetgogo.co.uk/?s=ipod
to return as:
http://www.gadgetgogo.co.uk/?s=ipod&post_type=product
So when using searches (slider banner and default WordPress search) it produces the second URL.
I was looking for similar solution as you but couldn't find any and at last I combined last few answers that I read here and got this working fine as I wanted.
Add below code in your theme's functions.php file
function wpb_change_search_url() {
if ( is_search() && ! empty( $_GET['s'] ) && ($_GET['post_type'] != 'product') ) {
wp_redirect( home_url( "/?s=" ) . urlencode( get_query_var( 's' ) ) . "&post_type=product" );
exit();
}
}
add_action( 'template_redirect', 'wpb_change_search_url' );
hope it will help you and others.
Apparently current WooCommerce versions only consider a search query a product search (and render using the appropriate template), if $query->is_post_type_archive( 'product' ) is true, so the key is to set not only the post_type, but the is_post_type_archive property as well, and to do it before WooCommerce loads its filter (default priority of 10), so with a priority of 9 or smaller.
Example to add into funtions.php:
function my_search_filter($query) {
if ( $query->is_search && ! is_admin() ) {
$query->set( 'post_type', 'product' );
$query->is_post_type_archive = true;
}
}
add_filter('pre_get_posts','my_search_filter', 9);
Please note, that this code will override all searches as product serach, so if you have other searches as well, implement appropriate checks at the begining of my_search_filter.
Just add this line to top of search.php
$_GET['post_type'] = 'product'
This can be done using pre_get_posts filter. Add below code in your theme's functions.php file
add_filter( 'pre_get_posts', 'search_by_product_only' );
function search_by_product_only( $query ) {
// check if search query only
if ( $query->is_search ) {
$query->set( 'post_type', array( 'product') ); // here you can add multiple post types in whcih you want to search
}
return $query;
}
I'm running the latest Wordpress with WooCommerce
I'm trying to customise my search results when I use the search bar in the header of my site.
This is how the results appear when doing a normal search:
http://www.sunshinetrading.com/snowmasters/?s=snow
This is how they should appear, when I search through WooCommerce.
http://www.sunshinetrading.com/snowmasters/?s=snow&post_type=product
What I need to do is automatically append &post_type=product onto every search query launched from the header.
My attempts at a solution:
I added this to my child theme's functions.php file, to try and append the query which would fix everything.
// Search WooCommerce
function search_filter($query) {
if ( !is_admin() && $query->is_main_query() ) {
if ($query->is_search) {
echo esc_url( add_query_arg( 'post_type', 'product' ) );
$query->set('post_type', 'product');
}
}
}
add_action('pre_get_posts','search_filter');
However, when I do this, and do a search, what the URL should be appears briefly as text on the page, before the website proceeds to load exactly the same page as before.
What am I doing wrong? Maybe I could solve this problem by editing the .htaccess file. I've added the following ...
# REWRITE SNOWMASTERS SEARCH
RedirectMatch 302 snowmasters.com.au/?s=(.*) http://snowmasters.com.au/?s=$1&post_type=product
This should redirect http://snowmasters.com.au/?s=snow to http://snowmasters.com.au/?s=snow&post_type=product
But it's not working?
I would appreciate your help Stack Overflow community :)
Thanks
Did you try this function already?
add_query_arg( 'post_type', 'product', 'http://www.sunshinetrading.com/' );
You can also register you query string var in wordpress like this:
function add_query_vars_filter( $qVars ){
$qVars[] = "post_type";
return $qVars;
}
add_filter( 'query_vars', 'add_query_vars_filter' );
This is from the codex: https://codex.wordpress.org/Plugin_API/Filter_Reference/query_vars
Anyone know how to remove default WordPress front page rel next and add custom rel next.
below rel next is wrong.because i am using pagination on my front page.
<link rel='next' title='About Us' href='http://myweb/about-us' />
i want to correct my rel next.any one can help me.
<link rel='next' href='http://myweb/page/2' />
using add_action , and remove_action
Recommended method:
I'm not sure that add_action and remove_action are the right tools for this, but since you are willing to use them, you should be willing to use add_filter - which is the right tool.
There are two filters that you can hook into:
add_filter( "previous_post_rel_link", 'remove_title_from_previous_link' );
add_filter( "next_post_rel_link", 'remove_title_from_next_link' );
Then, write your function to parse / remove the title attribute:
function remove_title_from_previous_link($link) {
// Write your code here to provide and return the correct link
// Sample only below:
return '<a href="my_custom_url" title="Corrected Url" rel="previous">';
}
function remove_title_from_next_link($link) {
// Write your code here to provide and return the correct link
// Sample only below:
return '<a href="my_custom_url" title="Corrected Url" rel="next">';
}
EDIT:
If you really must use add_action and remove_action, then you'd want to hook into this action like so:
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 999);
And then
add_action('wp_head', 'my_custom_rel_link_function');
And of course your custom function:
function my_custom_rel_link_function() {
// ... do your magic here ...
}
This is Only for Front Page
add below code in to the head section
// add custom link rel for home page
if(is_front_page()){
$post_per_page = 1;
$paged = (get_query_var('page')) ? get_query_var('page') : 1;
$published_posts = wp_count_posts()->publish;
$prev_link = get_pagenum_link( $paged - 1 );
$next_link = get_pagenum_link( $paged +1 );
if( $paged >= 2 ){
echo "<link rel=\"prev\" href=\"".$prev_link."\"/>"."\n";
}
if( $published_posts > ( $post_per_page * $paged ) ){
echo "<link rel=\"next\" href=\"".$next_link."\"/>"."\n";
}
}
Solution below:
This removes rel="next" & rel="prev" from custom post types and pages.
Add below code in function.php
add_filter( 'wpseo_next_rel_link', '__return_false' );
add_filter( 'wpseo_prev_rel_link', '__return_false' );