I want to add a specific word to every taxonomy archive.
I've this domain:
http://example.com/brand/nike/
And I want it to look like this:
http://example.com/nike-shop/
Is there any way to add the word "shop" to the slug and remove the taxonomy name (brand)?
I've checked to codex but I couldn't find anything.
EDIT: Changed the question to make it more clear
If you're using your custom taxonomy, you can try to add rewrite parameter to the code, like this:
register_taxonomy('taxonomy',
array('post'),
array(
...
'rewrite' => array(
'slug' => 'taxonomy-archive'
)
)
);
But once you change your code, do not forget to go to Settings-Permalinks and just click Save Changes button there.
If you can not affect on the register_taxonomy() function, you can try the solution based on this tutorial https://rudrastyh.com/wordpress/remove-taxonomy-slug-from-urls.html
Related
I'm looking for simple solution how to convert URL query like this:
?orderby=date-desc&query_type_color=or&filter_color=white,black&query_type_material=or&filter_material=iron&min_price=120&max_price=1190
into product loop query for WooCommerce. I prefer to use WP_Query or wc_get_products.
Please notice that query must contains all possible woocommerce product filter options like, order, orderby, taxonomy, attributes, min/max price, ...
I don't want to use any plugin.
Thanks
You can do it in the following way.
add_query_arg( array(
'orderby' => 'date-desc',
'query_type_color' => 'or',
'filter_color' => 'white'
........
.........
), 'http://example.com');
I had to create a custom post type, let's call it 'trucks'. The link to the archive of the posts looks like that: http://example.com/wpdirectory/?post_type=trucks
I want it to look like that: http://example.com/wpdirectory/trucks without the '?post_type=' query. How can I affect it?
I found out that it can be specified directly when registering new post type using register_post_type:
use the argument 'has_archive' => 'your-desired-archive-permalink'
in my case: 'has_archive' => 'trucks'
Try using the add_rewrite_rule() function is your functions.php file. Try this:
add_action( 'init', 'rewritePostType' );
function rewritePostType(){
add_rewrite_rule('^wpdirectory/([^/]*)/?','index.php?post_type=$matches[1]','top');
add_rewrite_tag('%post_type%','([^&]+)');
}
Otherwise you can overwrite the permastructures in Settings -> Permalinks in Wordpress and hard code it in.
I have a registered taxonomy (wps_type) and 2 custom post types (wps_business, wps_location) in that order. The registered taxonomy has rewrite arg set to slug => type and the post types have slug => business & slug => location already set. I need a custom smart permalink rewrite rule where all of the following are available.
Defaults Work (and still need to work):
http://domain.com/post-type/post-name => http://domain.com/business/post-name
http://domain.com/post-type/post-name => http://domain.com/location/post-name
http://domain.com/taxonomy/term-name => http://domain.com/type/term-name
But what I need is:
http://domain.com/post-type/taxonomy => http://domain.com/location-post-name/type-term-name
So I believe using post_type_link filter may not work, but I could be wrong. Here's some code that I've tried:
add_rewrite_rule( '^directory/([^/]*)/([^/]*)/?', 'index.php?wps_location=$matches[1]&wps_business=$matches[2]', 'top' );
add_rewrite_tag( '%wps_location%', '([^/]+)' );
add_rewrite_tag( '%wps_business_types%', '([^&]+)' );
Not sure if you ever found your answer, but I just found this article which outlines how to do this (though with a few caveats). Also, not sure if this answer would retain the third permalink structure you mentioned:
http://domain.com/taxonomy/term-name => http://domain.com/type/term-name
I personally rarely find the need for this structure, but often need the http://domain.com/post-type/tax/term structure, so that article works for me.
I created a new page which is assigned a custom template. When I visit that page's url I see what appears to be the default page layout (not my template) and the admin toolbar shows options pertaining to media (ex: Edit media).
After some head scratching, I deduced that somehow that url must point to a media item. I edited the page slug and "bingo" the actual page appears just as expected. When I visit the original url (from the first slug) I see the same media item.
Bottom line: It appears that coincidentally the page and the media item share the same name, and this was somehow causing WP's wires to get crossed.
My question: Can someone help me understand how/why this happens? Does wordpress create magic permalinks to everything in the media library (other than their location in wp-content/uploads/...)?
Note: The media item was uploaded normally into the media library (not FTP into the root directory, etc)
Yes, in WordPress you cannot have duplicate slugs/categories/taxonomies/tags. So if your theme allows for media files and permalinks to have their own page and the slug is the same as another one, it will usually append a number to it because the database does not like it.
media slug "example"
page slug "example" will not work since that slug exists already , if done in the admin it will automatically change the slug to "example-1".
I just had this problem and fixed it like this:
$post_s=get_posts('posts_per_page=-1');
foreach($post_s as $p){
$atts = get_posts('post_type=attachment&name='.$p->post_name.'&posts_per_page=-1&post_status=inherit');
foreach($atts as $att){
echo 'found!! '.$p->post_name;
// Update post 37
$my_post = array(
'ID' => $atts->ID,
'post_name' => $att->post_name.'-image'
);
// Update the post into the database
wp_update_post( $my_post );
}
}
This is a late answer, but I wanted to give a cleaner version of the answer that alesub gave.
function wp21418_append_to_post_name() {
// Checks to see if the option images_updated has been set or has a value of true.
if ( get_option( 'images_updated' ) === 'true' ) :
return;
endif;
// get all attachment posts.
$attachments = get_posts([
'post_type' => 'attachment',
'post_status' => 'inherit',
'name' => $p->slug,
'posts_per_page' => -1,
]);
// For each attachment, loop and update the post_name
foreach($attachments as $p){
$attachment = array(
'ID' => $p->ID,
'post_name' => $p->post_name.'-image'
);
// Update the post into the database
wp_update_post( $attachment );
}
// Once everything is looped, add the option to the database.
add_option( 'images_updated', 'true' );
}
add_action( 'after_setup_theme', 'wp21418_append_to_post_name' );
This function runs on an action hook right after the theme has setup. The first line checks to see if there is an option in the database images_updated. If that option exists, we bail on the function and it doesn't do any processing. Otherwise, if the option does not exist, it runs the function and sets the option at the very end.
This makes it so it will only run once. You don't have to remove the function after refresh. If you want to run it again, you can simply remove the if statement at the top. As a caveat: doing this will add another -image at the end of post_names even if they have -image already (e.g. -image-image)
There could be more file name checking for that situation. Will update the answer with that if someone really needs it.
I tried one of the suggested solutions, and ended up having attachment pages like /bob-image-image-image-image/.
I'd suggest that instead of using alesub or disinfor's code blindly, use a better option in the form of "Disable Media Pages" plugin.
https://github.com/joppuyo/disable-media-pages
It automatically sets all attachment slugs to an unique id, and there is the option to mangle any existing attachment slugs so they won't cause any issues in the future.
How can I tell the default WordPress search function to also look at the postmeta data?
For example, I have a postameta key called 'item_number' and if someone searches '113', I'd like for it to check the key's value as well as the post content.
Thanks!
A quick and dirty way to include meta values inside your searching scope: you can hook into pre_get_posts and add a meta_query like this:
Inside your functions.php:
add_filter( 'pre_get_posts', 'so_9224493_adjust_search_query');
function so_9224493_adjust_search_query( $query ) {
if ( !is_admin() && $query->is_search ) { //the !is_admin() boolean ensures that this does not affect your dashboard
$meta_query_arguments = array(
array(
'key' => 'item_number', //this is where you would put your meta key
'value' => $query->query_vars['s'],
'compare' => 'LIKE',
),
);
$query->set('meta_query', $meta_query_arguments);
};
}
The only problem with this route is that it will include the meta query so that your search query will only match posts that include the search term inside the Title/Content/Excerpt and the meta field, which might not be preferable for you.
You must replace your Wordpress search function with your own. Check this answer.
Very simple workaround: put all meta values you want to be searchable into post_exerpt field (use save_post action), so they will be found without any search modifications.
This site provided the answer I was looking for:
http://flav36rs.com/2010/03/15/extend-wordpress-search-to-include-custom-post-meta/
There are some helpful comments below the tutorial worth looking at if you run into problems.
EDIT:
The above link is dead.
Ultimately, I ended up writing a complete custom search query using JOINS to build the query I needed.