Searching custom taxonomies in wordpress - wordpress

Want to know is there any plugin/hack available for searching custom taxonomies in wordpress?
Directions for any idea will be great.

How about this?
function search_by_tax_filter(&$query)
{
if ($query->is_search)
$query->set('taxonomy', 'taxonomy_name');
}
add_action('parse_query', 'search_by_tax_filter');

Tricky business... This will get you started, this queries the top five tags (taxonomy: post_tag)...
// query the top five tags
$sql = '
SELECT wt.term_id ti,wt.name, wtt.count tc,wtr.term_taxonomy_id tti, wtr.object_id oi
FROM wp_terms wt
INNER JOIN wp_term_taxonomy wtt ON wt.term_id = wtt.term_id
INNER JOIN wp_term_relationships wtr ON wtr.term_taxonomy_id = wtt.term_taxonomy_id
LEFT JOIN wp_posts wp ON wp.ID = wtr.object_id
WHERE taxonomy = \'post_tag\'
GROUP BY name
ORDER BY count DESC LIMIT 0 , 5
';

I've recently developed a plugin that provides custom taxonomy filtering through front-end forms (with dropdowns and an optional text input):
Advanced Custom Post Search
It runs off of its own results page and doesn't directly tie in with the default search.php, nevertheless its still very flexible and allows you to override the plugin pages in your theme.

Related

Products with no category WordPress Panel

I am facing an issue. Inside wordpress panel at Products, some of them does not have any categories (not even uncategorized). I am playing arround via phpmyadmin Database and don't know if something I did messed up the categories.. How could I regenerate these products to have uncategorized category, or do something about it? Any guess or ideas?
*Furthermore, I am using a MySQL query in order to get the categories of a product via SKU:
$categories = $wpdb->get_results("
SELECT wp_term_relationships.term_taxonomy_id as category_id
FROM $wpdb->term_relationships
INNER JOIN $wpdb->postmeta ON $wpdb->postmeta.post_id = $wpdb->term_relationships.object_id
WHERE $wpdb->postmeta.meta_key = '_sku' AND ($wpdb->postmeta.meta_value = '$sku')
");
This query returned some results, but I see no categories in product, like the screenshot I have uploaded. Thanks in advance

How to get WordPress menu items based on theme location as simple string/text?

I want to show all menu and it's sub menu items form the WordPress database as simple text or string. No need to show a format. It could be object or array.
I am using this query but don't get the menu items:
global $wpdb;
$results = $wpdb->get_results( "SELECT *
FROM wp_terms AS t
LEFT JOIN wp_term_taxonomy AS tt ON tt.term_id = t.term_id
WHERE tt.taxonomy = 'nav_menu' ", ARRAY_A );
Use wp_nav_menu to receive the menu as an array, and walk_nav_menu_tree for a string.
All the db work is already done by WordPress, thus it is trusted to work also with future and past WP versions.
https://developer.wordpress.org/reference/functions/wp_nav_menu/
https://developer.wordpress.org/reference/functions/walk_nav_menu_tree/

Wordpress adds -2 to my post slug

Wordpress adds a -2 to my post slug. I know this can be caused by a post or attachment with the same name. It's a brand new blog with just 3 posts and 3 attachments, I could have checked for duplicates manually but used tools and a query just in case, to delete revisions, etc.
I used plugins Optimize Database after Deleting Revisions, Find Dupicates and Media Deduper, and it didn't work.
Plus I seached for duplicates on phmMyAdmin with query:
SELECT a.ID, a.post_title, a.post_type, a.post_status
FROM wp_posts AS a
INNER JOIN (
SELECT post_title, MIN( id ) AS min_id
FROM wp_posts
WHERE post_type = 'post'
AND post_status = 'publish'
GROUP BY post_title
HAVING COUNT( * ) > 1
) AS b ON b.post_title = a.post_title
AND b.min_id <> a.id
AND a.post_type = 'post'
AND a.post_status = 'publish'
And found no duplicates.
FYI I'm using plugins:
Akismet, Code Snippets, Genesis Simple Edits, Jetpack, and the ones mentioned above for database cleaning.
Any ideas on what I need to do to fix this issue?
I look forward to your replies. Thanks you very much.

How to move posts of a specific category into a custom post type

I want to move many posts which have more than one category into the custom post type.
Currently all posts are added into the posts section but i want to move all posts into my custom post type section my site is http://www.edarabia.com/
I want to move all school posts in the schools custom post type and all universities posts into the universities custom posts type and same for nurseries.
Please guide me how it can be possible.
You have to write SQL query changing the post_type field of posts or pages from "post" to "school" (assuming school is a custom post type name) and also map categories and tags.
Alternatively you can try plugins:
https://wordpress.org/plugins/post-type-switcher/
https://wordpress.org/plugins/convert-post-types/
The following update will change all posts types to school for posts that are in the category with ID 1 :
UPDATE
wp_posts
LEFT JOIN wp_term_relationships ON wp_posts.ID = wp_term_relationships.object_id
LEFT JOIN wp_term_taxonomy ON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
SET
wp_posts.post_type = 'school'
WHERE
wp_posts.post_type = 'post' AND
wp_term_taxonomy.taxonomy = 'category' AND
wp_term_taxonomy.term_id = 1

RSS Issue - Drupal to WordPress Migration

I'm using the script from this site to migrate a Drupal database to WordPress. It seems to work ok, except for the RSS feed which returns an HTTP 304 error when I try to access it.
I think that the problem is somewhere in the following lines:
TRUNCATE TABLE wordpress.wp_comments;
TRUNCATE TABLE wordpress.wp_links;
TRUNCATE TABLE wordpress.wp_postmeta;
TRUNCATE TABLE wordpress.wp_posts;
TRUNCATE TABLE wordpress.wp_term_relationships;
TRUNCATE TABLE wordpress.wp_term_taxonomy;
TRUNCATE TABLE wordpress.wp_terms;
When the script empties the WordPress database, RSS feeds stop working. However, if I carried out the import of blog posts without emptying the above tables, the feeds seem to work ok.
Any thoughts?
In the above line i think there is no any problem. So you have to check out all the code:
Database Conversion Table
Drupal 6.x Table(s) WordPress 2.9x Equivalent
term_data, term_hierarchy wp_terms
node, node_revisions `wp_posts
term_node wp_term_relationships
comments wp_comments
Truncate WordPress Tables
First, I needed to remove any data that is currently in certain wordpress tables so I could work with a fresh slate.
Note: By default when you install wordpress all tables are prefixed with wp_ unless you changed it to something else. The below queries will need to be modified if you used anything else other than wp_.
TRUNCATE TABLE wp_comments;
TRUNCATE TABLE wp_postmeta;
TRUNCATE TABLE wp_posts;
TRUNCATE TABLE wp_term_relationships;
TRUNCATE TABLE wp_term_taxonomy;
TRUNCATE TABLE wp_terms;
Import Taxonomy Terms
The next sets of queries imports taxonomy terms.
Note: Table names pre-pended with drupal. needs to be the actual name of your drupal database. You will need to change this to whatever you have your drupal database named.
INSERT INTO wp_terms (term_id, name, slug, term_group)
SELECT d.tid, d.name, REPLACE(LOWER(d.name), ' ', '-'), 0
FROM drupal.term_data d
INNER JOIN drupal.term_hierarchy h
USING(tid);
By default, WordPress has several taxonomy types available; categories, post_tag, and link_category. In my Drupal instance I used taxonomy primarily as tags, but you may have a different need. You may need to modify the 3rd line in the below query depending on how you want taxonomies imported:
Categories: category
Link Categories: link_category
Post Tags: post_tag
INSERT INTO wp_term_taxonomy (term_taxonomy_id, term_id, taxonomy,
description, parent)
SELECT d.tid, d.tid, 'post_tag', d.description, h.parent
FROM drupal.term_data d
INNER JOIN drupal.term_hierarchy h
USING(tid);
Import Post Content
Drupal allows for custom post types, while as of WordPress 2.9x, custom post types are only available via plugins. You can use the below query unmodified and it will convert all stories to posts, and everything else will transfer over as is. If you need to convert additional post types, you can add additional case statements.
Example:
WHEN 'book' THEN 'post'
I also adjusted the query so that ‘post_date_gmt’ would be populated correctly based on my GMT offset of -6:00 (Central Time). If you are in a different timezone you will need to adjust FROM_UNIXTIME(created+21600) to subtract or add correctly based on your location.
INSERT INTO
wp_posts (id, post_date, post_date_gmt, post_content, post_title,
post_excerpt, post_name, post_type, post_modified)
SELECT DISTINCT
n.nid, FROM_UNIXTIME(created),
FROM_UNIXTIME(created+21600), body, n.title, teaser, LOWER(n.title),
(CASE n.TYPE
WHEN 'story' THEN 'post'
ELSE n.TYPE
END) AS TYPE,
FROM_UNIXTIME(changed)
FROM drupal.node n, drupal.node_revisions r
WHERE n.vid = r.vid;
Import Post and Taxonomy Relationships
INSERT INTO wp_term_relationships (object_id, term_taxonomy_id)
SELECT nid, tid FROM drupal.term_node;
Category Count Updating
UPDATE wp_term_taxonomy tt
SET COUNT = (
SELECT COUNT(tr.object_id)
FROM wp_term_relationships tr
WHERE tr.term_taxonomy_id = tt.term_taxonomy_id
);
Import Comments
INSERT INTO wp_comments (comment_post_ID, comment_date,
comment_content, comment_parent, comment_author,
comment_author_email, comment_author_url, comment_approved)
SELECT nid, FROM_UNIXTIME(TIMESTAMP), comment, thread,
name, mail, homepage, STATUS
FROM drupal.comments;
Update Comment Count
UPDATE wp_posts
SET comment_count = (SELECT COUNT(comment_post_id)
FROM wp_comments
WHERE wp_posts.id = wp_comments.comment_post_id);
Update Post Slugs
Drupal’s URL aliases is equivalent to WordPress’ permalinks. Drupal has a much more aggressive title sanitation than WordPress. I wanted the ability to keep my titles the same for SEO reasons when migrating over to WordPress.
In order to keep my old titles, I need to hook into WordPress’ title sanitation with similar rules to Drupal. The below code will need to be placed somewhere in the functions.php file of your current theme.
add_filter('sanitize_title', 'my_sanitize_title');
function my_sanitize_title($title) {
$title = preg_replace('/\b(a|an|as|at|before|but|by|for|from|is|in|into|like|of|off|on|onto|per|since|than|the|this|that|to|up|via|with)\b/i', '', $title);
$title = preg_replace('/-+/', '-', $title);
$title = trim($title, '-');
return $title;
}
You will need to save the below code to a file i.e. “fix-slugs.php” in your main WordPress directory and run it through your browser.
< ?php
require_once('wp-load.php');
$posts = $wpdb->get_results(
"SELECT ID, post_title, post_name FROM $wpdb->posts"
);
$count = 0;
$ignored = 0;
$errors = 0;
foreach($posts as $post) {
if(strcmp($slug = sanitize_title($post->post_title), $post->post_name) !== 0) {
$wpdb->show_errors();
if(($result = $wpdb->query("UPDATE $wpdb->posts SET post_name='$slug' WHERE ID=$post->ID")) === false) {
$errors++;
} elseif($result === 0) {
$ignore++;
} else {
$count++;
}
} else {
$ignored++;
}
}
echo "<strong>$count post slug(s) sanitized.</strong><br />";
echo "$ignored post(s) ignored.<br />";
echo "$errors error(s).<br />";
If you were following along with this tutorial, I’ve made a few changes based on my Drupal setup using WordPress database description as a reference when I ran into issues. There may be some additional steps to be completed if you uploaded images through Drupal’s interface, but the above queries were able to successfully migrate my data from Drupal to WordPress.
Using HtmlAgilityPack for Visual Studio helped a lot in quickly building an importer to go through each of Drupal's posts and add them to Wordpress using XMLRPC. Since Drupal has two separate columns for the post content and post teaser, it is much better this way to import Drupal into Wordpress so that you can add <!--more--> tags on the fly.

Resources