Wordpress permalink issue - advanced search - php

I am having some issue on my wordpress site.
The site url is http://www.sharedmachine.in/.
On the home page there is search option.
Now the permalink structure right now is like this - http://www.sharedmachine.in/?p=123.
Now after clicking the search button, it suppose to redirect to the 'advanced search' page. but it doesn't.
So I changed the permalink structure to custom - /%postname%/.
Still it doesn't work.
The URL it should redirect to is
http://www.sharedmachine.in/advanced-search/?filter_search_type (some filters)
But instead it shows-
http://www.sharedmachine.in/?filter_search_type (filters)
The page name is not coming in the URL.
Now the code to get url is -
$adv_submit=wpestate_get_adv_search_link();
and the function -
function wpestate_get_adv_search_link(){
$pages = get_pages(array(
'meta_key' => '_wp_page_template',
'meta_value' => 'advanced_search_results.php'
));
if( $pages ){
$adv_submit = esc_url ( get_permalink( $pages[0]->ID) );
}else{
$adv_submit='';
}
return $adv_submit;
}
If I change the line
$adv_submit=wpestate_get_adv_search_link();
To
$adv_submit='http://www.sharedmachine.in/index.php/advanced-search/';
it works.
What can be the issue here?

aparently the $pages variable never gets populated. i would try something like:
$page = get_page_by_path('advanced-search');
if ($page) {
$adv_submit = esc_url ( get_permalink( $page->ID) );
} else {
// do something else
}
in this case you would be looking up the page by its slug as opposed to a page-meta you may or may not have set

Related

WordPress Pages - Adding subdirectory to permalink for specific pages

Currently, I have my permalink structure set up such as https://example.com/blog/%postname%/ which makes it so that whenever a blog post is loaded, the URL shows /blog/. I would also like to add the subdirectory /try/ for specific pages. I have tried to use a plugin to add categories to pages so I can create a category and add it that way, but it seems that this is not possible because the "Custom Structure" is already set as noted above.
Does anyone know of a way to add a subdirectory /try/ to specific pages for a WordPress website so the URL for certain pages would be such as https://example.com/try/page-title
Thanks.
First we have to add rewrite_rule
add_rewrite_rule(
'^try/([^/]+)/?',
'index.php?name=$matches[1]',
'top'
);
Then we need to use the post_link filter to change the link format for certain posts
add_filter( 'post_link', 'custom_post_permalink', 10, 4 );
function custom_post_permalink( $link, $post = 0 ) {
$post_cur_id = $post->ID;
$post_cur_slug = $post->post_name;
//array of post_ids with try in permalink
$try_array = array(1,7,22,78);
if(in_array($post_cur_id, $try_array)) {
$slug = '/try/' . $post_cur_slug . '/';
$link = home_url($slug, 'https');
}
return $link;
}
The array $try_array should contain post_id's in which we change the link format

wordpress dynamic urls witout redirect

I need to create dynamic urls all loading same page (please note loading not redirecting) plugins that I could find only do redirects. Basically what I need is:
/somepage/something
/somepage/anotherthig
/somepage/thething/morethings
all loading existing page
/somepage
but the original url must be kept (not a redirect). Any advice on how t do it ( a plugin that does this works as well if you know of one) is greatly appreciayed.
Shouldn't be that hard and can be achieve by modifying $wp_query and $post global variable,
try this code
// modify variable by hooking it on 'wp' action
add_action( 'wp', function() {
global $wp, $wp_query, $post; //define global variable
//include $wp variable so you can check the url request
// list the url you want to use
$dynamic_url = [
'somepage/something',
'somepage/anotherthig',
'somepage/thething/morethings'
];
// check if page request is found from the array above
if ( in_array( $wp->request, $dynamic_url ) ) {
// build query argument
$args=[
'post_type' => 'page', //assuming its a page
'p' => 26 // page ID of the page you want to display on those dynamic URLS
];
// run the query and assign it to $wp_query global variable
$wp_query = new WP_Query( $args );
// modify is_single wp_query param and tell it its not a post
$wp_query->is_single = '';
// modify is_page wp_query param and tell it its a page
$wp_query->is_page = 1;
//assign (1st) found post to global post variable
$post = $wp_query->posts[0];
//modify header as 202 status (unless you want these pages to stay as 404), by defualt its a 404
status_header( 202 );
//done
}
});
You can use this plugin to create dynamic url's. this plugin is free.
https://wordpress.org/plugins/sdk-wp-dynamic-url/
if you are looking something advance then this plugin will surely do for you but its paid:
https://wordpress.org/plugins/if-so/
Hope that Helps
Happy Coding

Wordpress - Customise Search 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

wordpress get all publish pages permalinks

I want to get the permalinks of all published pages and export it to an excel file.
What is the best solution?
Should I go to wp-admin panel and copy-paste the permalinks from the on Page editor?
Can I get the export data using a mysql query?
It is possible but very difficult to do via a SQL query, because WordPress allows you to change your Permalink structure - so the SQL query would need to regard all permalink options to build the correct permalink when the query is executed.
And I guess you are looking for a way that does not require you to copy paste links from the admin page ;-)
By far the best and easiest thing to do is write a small script that runs the export for you within WordPress and uses the functions get_pages and get_permalink:
// Get a list of all published pages from WordPress.
$pages = get_pages( 'post_status=publish' );
// Loop through all pages and fetch the permalink for each page.
foreach ( $pages as $page ) { //fixed this too
$permalink = get_permalink( $page->ID );
// Do something with the permalink now...
}
Note: This code will only run inside WordPress, i.e. as as Plugin or inside a Theme. Also how to do the excel export is beyond the scope of my answer...
Personally I'd create a WordPress plugin (there are many guides out there on how this works, like this one).
In the plugin you could simply check for a URL param, and if this param is present then export the data. Additionally I would check if the user that requests the export has admin permissions before exporting the data.
A bit like this:
/**
* The WordPress plugin header...
*/
$allowed = false;
$requested = false;
// Check if the current user has admin capabilies.
if ( current_user_can( 'manage_options' ) ) { $allowed = true; }
// Check if the user requested the export.
// i.e. by calling URL http://yoursite.com?export_permalinks=1
if ( 1 == $_GET['export_permalinks'] ) { $requested = true; }
if ( $requested && $allowed ) {
// 1. Get a list of all published pages from WordPress.
$pages = get_pages( 'post_status=publish' );
// 2. Build the export data.
$export = array();
foreach ( $pages as $page ) {
$permalink = get_permalink( $page->ID );
$export[] = array( $page->ID, $permalink );
}
// 3. Export the data (I just var_dump it as example).
var_dump( $export );
// We're done, so don't execute anything else.
exit;
}
Please note that this code should only explain my suggested workflow. It does not use best practices and I don't recommend to use it like this on a live site

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