Wordpress - Customise Search Query - php

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

Related

WordPress Pagination problem for one category - pagination fix

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.

How to replace Wordpress default search with Woocommerce search

I need help replacing the default Wordpress search with Woocommerce search.
Tried this in functions.php
add_filter( 'pre_get_posts', 'custom_pre_get_posts' );
function custom_pre_get_posts( $query ) {
if ( is_search() ) {
$query->set('post_type', 'product');
}
return $query;
}
but the output of the search is still just a list of post-like search results. I would like the same output (product archive'ish) as the Woocommerce search outputs.
Is that possible?
Found a plugin. If anyone else wants a solutions without coding: https://wordpress.org/plugins/woocommerce-menu-extension/

Manipulate slugs and urls on wordpress

Is there an away to create "virtual" slugs on wordpress?
For example:
I create a custom post type named "year" and the slug is /year/. This custom post type was only created to generate the slugs(/year/). I don't have any post inside this custom post type.
I want to type url /year/1988/ and the Wordpress call the single.php file.
If I do it today the wordpress will return 404 because 1988 needs to be a post inside the custom post type(year).
I need some .php code or .htaccess to do something like: If type something after /year/ its load the single file because I can't create a conditional for each year, 1901, 1902, 1903 ... and I will take this information and make the necessary query.
If you need this to work on your home page only (ie. mysite.org/years/1988) you can add a custom rewrite tag and rule:
add_filter( 'init', function() {
add_rewrite_tag( '%years%', '([^&]+)' );
// Match the front page and pass years value as a query var.
add_rewrite_rule( '^years/([^/]*)/?', 'index.php?years=$matches[1]', 'top' );
});
Then use the new query var to load a different template:
add_filter( 'template_include', function( $template ) {
if ( get_query_var( 'years' ) ) {
$new_template = locate_template( array( 'single.php' ) );
if ( !empty( $new_template ) ) {
return $new_template;
}
}
return $template;
});
I tried to use year, but it wouldn't work, I think it's a reserved query var used by Wordpress. And don't forget to flush your toilet, I mean rewrite rules ;).

WordPress query string to change SEO friendly URL

I want to create SCO friendly URL in the WordPress. Also, I have added a hook in my functions.php file as below code but not working.
My URL:http://bddlabs.com/webinar01/?id=pzlyglhbepotwpvbigdatadim_data&storyid=Story_997e9391-71d7-4fac-804b-de891c7aa595
I want http://bddlabs.com/webinar01/pzlyglhbepotwpvbigdatadim_data/Story_997e9391-71d7-4fac-804b-de891c7aa595
Is it possible? I am not sure about it. URL query string is coming from the third party, URL query string is coming from the third party, page content render based on query string.
Below code added landing-page-15.php it WordPress theme base page template
function fb_change_search_url_rewrite() {
if ( ! empty( $_GET['storyid'] ) ) {
wp_redirect( home_url( "/webinar01/" ) . urlencode( get_query_var( 'storyid' ) ) );
load_template( realpath(__DIR__ . '/..') . '/landing-page-15.php' );
exit();
}
}
add_action( 'template_redirect', 'fb_change_search_url_rewrite' );
//additional rewrite rule + pagination tested
function rewrite_search_slug() {
set_query_var('storyid', $_GET['storyid']);
add_rewrite_rule(
'find(/([^/]+))?(/([^/]+))?(/([^/]+))?/?',
'index.php?id=$matches[2]&storyid=$matches[6]',
'top'
);
}
add_action( 'init', 'rewrite_search_slug' );
You may consider using WordPress builtin permalinks option located in Settings tab.
Set %category%/%postname%/ so that permalink should work as per your requirements.
When posting new content, select a sub category and put post title the same you want to appear in your permalink.
Hope it answers your question.
Wordpress can do this by default. https://codex.wordpress.org/Using_Permalinks

Setting a custom canonical URL in Yoast Wordpress SEO Plugin

Reposted due to no replies.
I'm having some trouble setting a custom canonical title using the Wordpress SEO API: http://yoast.com/wordpress-seo-api-docs/
I have a custom post type called designs which uses a custom URL rewrite. It takes the base page /design/ and adds the design name to it like /design/a-design/. The canonical in Wordpress SEO by default is the /design/ page.
What I want to do is write a function which determines if it is a design page and return a different canonical. I can test whether it's a design page by doing if ($design == ""){ and I tried to use the custom permalink URL, but the function just removes the canonical completely.
Here's my basic function:
function design_canonical(){
if ($design == "") {
// Leave blank and Yoast SEO will use default canonical for posts/pages
}
else {
return $design['detailslink'];
}
}
add_filter( 'wpseo_canonical', 'design_canonical' )
Quite clearly doing something wrong, but I'm not entirely sure what.
Thoughts?
You could try something like:
function design_canonical($url) {
global $post;
if ( get_post_type( $post->ID ) == 'design' ) {
return site_url( '/design/' . $post->post_name );
} else {
// Do nothing and Yoast SEO will use default canonical for posts/pages
return $url;
}
}
add_filter( 'wpseo_canonical', 'design_canonical' );
Hi i couldn't answer to the above post so I just make another one.
I tried to use the answer from stealthyninja for a similar problem and it almost worked. Except the last part: the empty else statement. It renders no output if the rule doesn't match.
Maybe Yoast updated his plugin on this within the last 2 years so I thought I should mention it here.
The following Code-Snippet solved my problem:
function design_canonical($url) {
global $post;
if ( get_post_type( $post->ID ) == 'design' ) {
return site_url( '/design/' . $post->post_name );
} else {
return $url;
}
}
add_filter( 'wpseo_canonical', 'design_canonical' );

Categories