Wordpress adds -2 to my post slug - wordpress

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.

Related

woocommerce variable products database structure

I'm building a wordpress eshop using woocommerce and I want to integrate it with an existing erp tool. I have complete the 90% of all the connections but I'm facing a problem with variable products and I can't find a solution by reading the documentation. I need some more info about how to create a variable product through the erp.
Below is what I'm currently doing:
I am creating an entry in wp_posts with post_type product and for the variation and I'm creating another entry on wp_posts with post_type product_variation and post_parent the ID of the product that holds the variation.
I believe that I'm missing something because doing only the above is not working as expected.
I will write what I am using.
update wp_postmeta set meta_value = 270 where post_id in (
select id from wp_posts where post_parent = 46 and post_type = 'product_variation'
) and meta_key = '_regular_price';
... where 270 is my new price for all variable products.
... using select to test the solution:
select * from wp_postmeta where post_id in (
select id from wp_posts where post_parent = 46 and post_type = 'product_variation'
) and (meta_key = '_regular_price' or meta_key = '_sale_price');
You can see all meta_key related to the wp_posts with:
select distinct meta_key from wp_postmeta where post_id = XX;
In my case, the 46 is the post_id. For you see what is your, open the product in wp-admin.
I hope that I was helpfull.
I think you might be getting the types wrong. Variations are of the post_type "product_variation" not "product".
Also make sure you have the appropriate wp_postmeta values. For example if you have a product variation that has no price it will actually not work during checkout even if it's a free product.
To see what wp_postmeta values you need, simply search that table for a product id that you got from a product that you created through the WC back-end.

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.

Wordpress wpdb query issue - how to filter out revisions?

Can someone tell me how to modify the code below so that it filters out revisions? I tried using revision deletion plugins but they only work for revisions of posts or pages... not custom post types.
<?php
global $wpdb;
global $post;
$review_id = $post->ID;
if (get_post_type($review_id) == "features") {
$assoc_id = get_post_meta($review_id, "associated_hosts_value", true);
$review_id = intval($assoc_id);
}
$assoc = $wpdb->get_col("SELECT DISTINCT post_id from wp_postmeta WHERE (meta_key='associated_hosts_value') AND ((meta_value LIKE ',{$review_id},%') OR (meta_value LIKE '%,{$review_id},%') OR (meta_value = '{$review_id}') OR (meta_value LIKE '%,{$review_id}'));");
Revision is stored in wp_posts under the post_type column, so you will need to inner join on wp_posts and query for a post_type NOT IN ('revision'):
"SELECT DISTINCT post_id from {$wpdb->postmeta} INNER JOIN {$wpdb->posts} ON ID = post_id WHERE post_type NOT IN ('revision') ..."
By the way, inserting raw SQL queries in to get_col is a bad practice, as it leaves you vulnerable to SQL injections. Use $wpdb->prepare() as shown here:
http://codex.wordpress.org/Class_Reference/wpdb#Protect_Queries_Against_SQL_Injection_Attacks
Note that you will need to escape the % used in the LIKE statements by changing them to %% to work within prepare().
Also, you shouldn't hard code WP table names in to your query statements. The wpdb class stores table names for use in constructing your queries, i.e. $wpdb->posts, $wpdb->postmeta. Using these assures that your plugin will always query the correct WP tables. Try your code in a multi site installation to find out why this is important.

MYSQL Statement INNER JOIN with multiple condition

I have a WP site where I've also got a few php pages outside of my WP installation which I need to query the database that powers my WP site. I have a working query but it doesn't show what I really need output.
These are the two tables wp_posts and wp_postmeta
wp_posts
ID
post_author
post_date
post_date_gmt
post_content
post_title
post_excerpt
post_status
comment_status
pint_status
post_password
post_name
to_ping
pinged
post_modified
post_modified_gmt
post_content_filtered
post_parent
guid
menu_order
post_type
post_mine_type
comment_count
wp_postmeta
meta_id
post_id
meta_key
meta_value
and these is the query that show the publish post:
$query = "SELECT post_title, post_name FROM wp_posts WHERE post_type = 'post' AND post_status = 'publish' ORDER BY post_date DESC LIMIT 5";
and these is the query that show with the meta_key= '_wp_attached_file'
$query ="SELECT post.ID, post.post_title, post.post_excerpt, post.post_content, meta.meta_value FROM wp_posts AS post INNER JOIN wp_postmeta AS meta ON meta.post_id = post.id AND meta.meta_key = '_wp_attached_file' ";
what I want is to combine these two query to show post_status ='publish' and the meta_key = '_wp_attached_file'
here is the example of my query but doesn't show value
<?php
$query ="SELECT post.ID, post.post_title, post.post_excerpt, post.post_content, meta.meta_value FROM wp_posts AS post INNER JOIN wp_postmeta AS meta ON meta.post_id = post.id AND meta.meta_key = '_wp_attached_file' WHERE post_type = 'post' AND post_status = 'publish' ORDER BY post_date DESC LIMIT 5 ";
$result = mysql_query($query,$link);
if(mysql_num_rows($result)) {
while($post = mysql_fetch_object($result)) {
echo "$post->meta_value";
echo "$post->post_title";
echo "$post->post_content";
}
}
?>
many thanks guys.. .
another problem it show only 1 post and does not latest post
here is my updated code
<?php
$con = mysql_connect("localhost", "root", "");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db("wp_db",$con);
$sql = "SELECT
posts.ID,
posts.post_title AS title,
posts.post_content AS content,
files.meta_value AS filepath
FROM
wp_posts posts
INNER JOIN wp_posts attachments ON posts.ID = attachments.post_parent
INNER JOIN wp_postmeta files ON attachments.ID = files.post_id
WHERE 1 = 1
AND files.meta_key = '_wp_attached_file'";
$result = mysql_query($sql,$con);
while ($row = mysql_fetch_object($result))
{
echo $row->title . "<br />";
}
mysql_close($con);
?>
do I get something wrong with my code.. .thank for help thank you so much.. .
Hi #idontknowhow:
Your problem appears to be invalid assumptions regarding WordPress' database schema. WordPress stores it's attachments as a post_type='attachment'. Run this query to see what I mean:
SELECT
wp_posts.ID,
wp_posts.post_type
FROM
wp_posts
INNER JOIN wp_postmeta ON wp_posts.ID = wp_postmeta.post_id
WHERE 1 = 1
AND wp_postmeta.meta_key = '_wp_attached_file'
Records in wp_posts with a post_type='attachment' will typically have 'post_status='inherit' and 'post_parent' equal to your post's ID field value.
Now I didn't completely follow what you were looking to accomplish. You mentioned the technical problems you were having and but not the actual result you were trying to achieve. Without knowing the latter it's hard for me to verify that I understood your question. So I may have misunderstood what you were looking for, but I think this is what you wanted:
SELECT
posts.ID,
posts.post_title AS title,
posts.post_content AS content,
files.meta_value AS filepath
FROM
wp_posts posts
INNER JOIN wp_posts attachments ON posts.ID = attachments.post_parent
INNER JOIN wp_postmeta files ON attachments.ID = files.post_id
WHERE 1 = 1
AND files.meta_key = '_wp_attached_file'
But using direct SQL is frowned upon in the WordPress world; instead use of the WordPress API is the preferred choice if it provides what you need. There are many reasons for this; i.e. to avoid breakage if future WordPress versions change the database schema and because WordPress does a lot of data caching so it might be more performant in everyday usage to not use direct SQL and to use the WordPress API instead.
However, it is not 100% straightforward to use the WordPress API to address your question, though it can be done. If you'd like to see how may I suggest asking over at StackOverflow's sister site WordPress Answers where lots of WordPress enthusiasts can help?
Hope this helps.
-Mike
P.S. I find that a lot of questions about querying the WordPress database here on StackOverflow have people attempt to answer them even though they lack knowledge of the WordPress database schema and more importantly, are not aware of the WordPress API. People here know MySQL really well but often don't know WordPress well enough to give the right answer on WordPress-related questions. WordPress Answers is probably a better place to ask questions about WordPress.
Your query looks correct.
Are you sure you have published posts with _wp_attached_file?
Do the outputs of your queries above have any common records?
Did you tryed to filter mete.meta_key on WHERE? Something like that:
SELECT post.ID, post.post_title, post.post_excerpt,
post.post_content, meta.meta_value
FROM wp_posts AS post
INNER JOIN wp_postmeta AS meta ON meta.post_id = post.id
WHERE meta.meta_key = '_wp_attached_file'
AND post.post_type = 'post'
AND post.post_status = 'publish'
ORDER BY post.post_date DESC
LIMIT 5;

Searching custom taxonomies in 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.

Resources