I had to copy a whole wordpress-installation to another server. I simply installed a new Wordpress there and imported all the data from an xml-file previous Wordpress "exported" for me. Anyway, custom menus were not included. Do you know where in the database are they located, so I could grab them from there?
I've got a site with a Magento install and WordPress install sitting next to each other, and cross-linking.
I recently spent several hours writing a class to drop into the Magento installation so that I could render the WordPress menu as the navigation menu in the Magento site.
Posts here have been helpful, but none of them have completely explained the structure of how WordPress menus are stored. Like many WP things, it is stored in a series of relationships. Here's the structure:
(note that this example presumes a table prefix if of "wp_")
First, it's important to recognize that a menu item can be a post (technically it's a page, but pages are stored in the post table), a category, or it can be a custom link.
Because WP supports multiple menus, you first look in the wp_term_taxonomy table table to find any terms with the taxonomy of 'nav_menu'. Note the term_id from that table.
To find the name and slug of the menu, visit the wp_terms table and find the term with the id noted from step 2 above.
Go to wp_term_relationships table and list all of the records with the term_taxonomy_id that matched the term_id from step 1. The object_id field tells you the wp_post.id record where you can find the menu record.
Finally, go to wp_postmeta to find many elements describing the menu. Of particular interest are:
_menu_item_object - the TYPE of menu item (page, custom, or category)
_menu_item_object_id - the id of the actual POST (or category, if it's a category) that the menu item references
_menu_item_menu_item_parent - the heirarchical parent structure of the MENU (which can be different than the post parent relationships)
_menu_item_url - the slug of the menu item (if it is a custom link menu item)
Sample SQL statements to perform the above described operation:
SELECT t.term_id
FROM wp_term_taxonomy as tax
LEFT JOIN wp_terms as t ON tax.term_id = t.term_id
WHERE taxonomy = 'nav_menu' and name like '%top%'
(looks for a menu item with the name of 'Top', and gets the term id)
SELECT p.ID, p.post_title, p.post_name, p.menu_order, n.post_name as n_name, n.post_title as n_title, m.meta_value, pp.meta_value as menu_parent
FROM wp_term_relationships as txr
INNER JOIN wp_posts as p ON txr.object_id = p.ID
LEFT JOIN wp_postmeta as m ON p.ID = m.post_id
LEFT JOIN wp_postmeta as pl ON p.ID = pl.post_id AND pl.meta_key = '_menu_item_object_id'
LEFT JOIN wp_postmeta as pp ON p.ID = pp.post_id AND pp.meta_key = '_menu_item_menu_item_parent'
LEFT JOIN wp_posts as n ON pl.meta_value = n.ID
WHERE txr.term_taxonomy_id = 3 AND p.post_status='publish'
AND p.post_type = 'nav_menu_item' AND m.meta_key = '_menu_item_url'
ORDER BY p.menu_order
(loads the data for the menu, based on the term_id of 3)
Note that this sql statement will work for pages and custom menus (I don't have any categories, so didn't include that). The data loaded will allow you to build the permalink using the siteurl from the wp_options table, and appending the post_name to the end (technically, it's not getting the parent structure, but WP finds the page/post properly without it)
Update
A commenter asked about assembling the child menu items with the parent menu items. That will need to be done with PHP. Something like below will do that for you:
// run the query from above
$results = $wpdb->get_results('SELECT....');
// declare new variable to store "assembled" menu
$menu = array();
// loop over the items assigning children to parents
foreach( $results AS $row ) {
// assemble key bits for the menu item
$item = array(
// handles custom navigation labels
'title' => ( $row->post_title ) ? $row->post_title : $row->n_title,
// handles custom links
'permalink' => ( $row->meta_value ) ? $row->meta_value : get_permalink( $row->ID ),
// declares empty placeholder for any child items
'children' => array()
);
// if the menu item has a parent, assign as child of the parent
if ( $row->menu_parent ) {
$menu[ $row->menu_parent ][ 'children' ][] = $item;
} else {
$menu[ $row->ID ] = $item;
}
}
var_dump( $menu );
// outputs something like below:
/**
* array (size=6)
* 77 =>
* array (size=3)
* 'title' => string 'About Us' (length=8)
* 'permalink' => string 'http://www.example.com/about' (length=33)
* 'children' =>
* array (size=7)
* 0 =>
* array (size=3)
* 'title' => string 'Welcome' (length=22)
* 'permalink' => string 'http://www.example.com/welcome' (length=35)
* 'children' =>
* array (size=0)
* empty
* 1 =>
* array (size=3)
* 'title' => string 'Mission' (length=20)
* 'permalink' => string 'http://www.example.com/mission' (length=33)
* 'children' =>
* array (size=0)
* empty
* 90 =>
* array (size=3)
* 'title' => string 'Contact Us' (length=10)
* 'permalink' => string 'http://www.example.com/contact' (length=33)
* 'children' =>
* array (size=5)
* 0 =>
* array (size=3)
* 'title' => string 'Why Us' (length=12)
* 'permalink' => string 'http://www.example.com/why' (length=35)
* 'children' =>
* array (size=0)
* empty
* 1258 =>
* array (size=3)
* 'title' => string 'Login' (length=12)
* 'permalink' => string 'https://customlink.example.com/some/path/login.php' (length=82)
* 'children' =>
* array (size=0)
* empty
*/
For people still arriving at this question, I'll put it in simple phpMyAdmin terms.
There are 6 tables involved.
1. wp_term_taxonomy
WordPress keeps each nav menu location as a record in the 'wp_term_taxonomy' table, but the only unique identifier there is a number ID; 1, 2, 3, etc.
You can also see the 'count' figures that show the number of items in each menu location.
This is what's created when you type
register_nav_menu('your-navmenu', 'Your Navmenu');
You will not find the name 'Your Navmenu' anywhere in the database – that is just used for labelling it in the interface.
However, you will find 'your-navmenu' elsewhere.
2. wp_terms
Your actual menus get stored in 'wp_terms'.
WordPress is confusing with this terminology. In the interface, it has 'display locations' and 'menus'. The location is what's created by register_nav_menu(). It's best to consider the 'menus' in the interface as lists of pages.
I've given them the name 'navlist' here.
The menus also get their own IDs, which are often the same as the location IDs (because people often create one menu for one location at the same time), which can get confusing.
This item is created by the 'Menus' page in the admin interface:
3. wp_options
You will see the 'your-navmenu' slug in 'wp_options'. Here, WordPress stores your current settings for the nav menus. It stores it in a serialised array (using PHP's serialize() function).
The original array (obtained using unserialize()) looks like this.
array (
'custom_css_post_id' => 56,
'nav_menu_locations' =>
array (
'your-navmenu' => 2,
'another-navmenu' => 3,
),
)
This changes according to which menu (by its ID; '2', '3') you set to which location (by its slug; 'your-navmenu', 'another-navmenu').
4. wp_posts
The menu entries themselves are stored in a different table, 'wp_posts'.
You can find these by searching for 'nav_menu_item':
I've cut out most of the columns for brevity and kept the relevant ones.
Here, you can see the 'menu_order' column, which stores their order in the menu they are in.
The menu entries are stored like actual posts, with a post ID and URL (but it will bring a 404 if you visit it, and they have no 'post_content').
All menu items stored as a sub-item will have a 'post_parent' ID. This is the ID of the actual page that their parent links to, not its menu item ID.
5. wp_postmeta
Menu items are linked to their respective pages in the 'wp_postmeta' table.
The menu item ID ('post_id') is stored in relation to the post ID ('meta_value') in the '_menu_item_object_id' rows, while sub-items are linked to their parent items in the '_menu_item_menu_item_parent' rows.
It's easy to get confused here.
'post_id' is the ID of the menu item, not the post. 'meta_value' is the ID of the post, not the menu item, in '_menu_item_object_id' rows, but it's the ID of the parent menu item, not a post, in '_menu_item_menu_item_parent' rows.
6. wp_term_relationships
The links between each menu item and each menu location are stored in 'wp_term_relationships'.
Here, 'object_ID' is the post ID of the menu item (as seen in 'wp_posts'), and 'term_taxonomy_id' is the ID of the menu location (as seen in 'wp_term_taxonomy').
Hope this cleared it up for some people. I know I was very confused at the start.
I found this just because I was looking for the answer myself.
I see your post is quite old, but the answer is in wp_postmeta, run this query:
SELECT *
FROM `wp_postmeta`
WHERE meta_key LIKE '%menu%'
LIMIT 0, 30
You'll find many entries.
This setting happens in the wp_posts table.
Look in the table for records where the menu_order greater than zero.
select * from wp_posts where menu_order > 0;
It will also give you the name of the option in the wp_options table where the menu option is configured.
select * from wp_options where option_name = "nav_menu_options";
Also be aware that that wordpress import/export tool will not import media (images,video etc) from the media library which are not being used in posts. If you've got stuff that you directly linked to, its not going to be moved either.
I have been searching high and low for the entire structure and I finally cracked the code:
SELECT
p.ID,
m.meta_value,
md.post_author,
wp_users.user_nicename,
p.post_parent,
p.menu_order,
md.post_title
FROM
wp_posts AS p
INNER JOIN wp_postmeta AS m ON m.post_id = p.ID
INNER JOIN wp_posts AS md ON md.ID = m.meta_value AND m.meta_value = md.ID
INNER JOIN wp_users ON md.post_author = wp_users.ID
WHERE
p.menu_order > 0 AND
p.post_type = 'nav_menu_item' AND
m.meta_key = '_menu_item_object_id'
ORDER BY
p.menu_order ASC
To simplify/complexify #random_user_name's two queries, you can condense them into 1 query so that the only input parameter needed is the menu name. Like this:
SELECT p.ID, p.post_title, p.post_name, p.menu_order,
n.post_name as n_name, n.post_title as n_title,
m.meta_value, pp.meta_value as menu_parent
FROM wp_term_taxonomy as tax
INNER JOIN wp_terms as t ON tax.term_id = t.term_id
INNER JOIN wp_term_relationships as txr ON t.term_id = txr.term_taxonomy_id
INNER JOIN wp_posts as p ON txr.object_id = p.ID AND p.post_status = 'publish' AND p.post_type = 'nav_menu_item'
LEFT JOIN wp_postmeta as m ON p.ID = m.post_id AND m.meta_key = '_menu_item_url'
LEFT JOIN wp_postmeta as pl ON p.ID = pl.post_id AND pl.meta_key = '_menu_item_object_id'
LEFT JOIN wp_postmeta as pp ON p.ID = pp.post_id AND pp.meta_key = '_menu_item_menu_item_parent'
LEFT JOIN wp_posts as n ON pl.meta_value = n.ID
WHERE tax.taxonomy = 'nav_menu'
AND t.name like '%main nav%'
ORDER BY p.menu_order
This SQL statement gets the job done a little more thoroughly and legibly than the rest, I think. All you need to do is put your menu name into t.name = 'main nav' on the first join. It outputs every tidbit of menu info except the XFN field.
SELECT t.name as 'menu name',
p1.ID as 'post id', p1.post_title as title, p1.post_name as slug, p1.menu_order as 'order',
m1.meta_value as classes, m2.meta_value as menu_item_parent, m3.meta_value as object, m4.meta_value as object_id,
m5.meta_value as target, m6.meta_value as type, m7.meta_value as url,
p2.post_title as 'title-2', p2.post_name as 'slug-2'
FROM wp_term_taxonomy as tax
INNER JOIN wp_terms as t ON t.term_id = tax.term_id AND t.name = 'main nav'
INNER JOIN wp_term_relationships as txr ON txr.term_taxonomy_id = t.term_id
INNER JOIN wp_posts as p1 ON p1.ID = txr.object_id AND p1.post_status = 'publish' AND p1.post_type = 'nav_menu_item'
LEFT JOIN wp_postmeta as m1 ON m1.post_id = txr.object_id AND m1.meta_key = '_menu_item_classes'
LEFT JOIN wp_postmeta as m2 ON m2.post_id = txr.object_id AND m2.meta_key = '_menu_item_menu_item_parent'
LEFT JOIN wp_postmeta as m3 ON m3.post_id = txr.object_id AND m3.meta_key = '_menu_item_object'
LEFT JOIN wp_postmeta as m4 ON m4.post_id = txr.object_id AND m4.meta_key = '_menu_item_object_id'
LEFT JOIN wp_postmeta as m5 ON m5.post_id = txr.object_id AND m5.meta_key = '_menu_item_target'
LEFT JOIN wp_postmeta as m6 ON m6.post_id = txr.object_id AND m6.meta_key = '_menu_item_type'
LEFT JOIN wp_postmeta as m7 ON m7.post_id = txr.object_id AND m7.meta_key = '_menu_item_url'
LEFT JOIN wp_posts as p2 ON p2.ID = m4.meta_value AND p2.post_status = 'publish' AND m6.meta_value = 'post_type'
WHERE tax.taxonomy = 'nav_menu'
ORDER BY p1.menu_order
I added additional column on cale_b's query. The column lvl has depth for indentation.
Assume that:
max depth is 6, so i defined 6 variables.
menu's post_id is over 6. Most of you would've already exceed that number.
Query:
select case when a.meta_value = 0 then least(#lvl:=1, #p1:=a.id)
when a.meta_value = #p1 then least(#lvl:=2, #p2:=a.id)
when a.meta_value = #p2 then least(#lvl:=3, #p3:=a.id)
when a.meta_value = #p3 then least(#lvl:=4, #p4:=a.id)
when a.meta_value = #p4 then least(#lvl:=5, #p5:=a.id)
when a.meta_value = #p5 then least(#lvl:=6, #p6:=a.id)
end lvl
,a.id
from (
SELECT p.id
,p.post_title
,p.post_name
,pp.meta_value
,p.menu_order
,n.post_name as n_name
,n.post_title as n_title
,m.meta_value meta2
FROM wordpress_dw.stat_term_relationships as txr
INNER JOIN wordpress_dw.stat_posts as p ON txr.object_id = p.ID
LEFT JOIN wordpress_dw.stat_postmeta as m ON p.ID = m.post_id and m.meta_key = '_menu_item_url'
LEFT JOIN wordpress_dw.stat_postmeta as pl ON p.ID = pl.post_id AND pl.meta_key = '_menu_item_object_id'
LEFT JOIN wordpress_dw.stat_postmeta as pp ON p.ID = pp.post_id AND pp.meta_key = '_menu_item_menu_item_parent'
LEFT JOIN wordpress_dw.stat_posts as n ON pl.meta_value = n.ID
,(select #lvl:=0, #p1:=0, #p2:=0, #p3:=0, #p4:=0, #p5:=0, #p6:=0) x
WHERE txr.term_taxonomy_id = 2 -- your menu term id
AND p.post_status='publish'
AND p.post_type = 'nav_menu_item'
ORDER BY p.menu_order
) a
Result:
lvl |id |
----|----|
1 |508 |
1 |509 |
2 |510 |
3 |511 |
3 |512 |
3 |513 |
3 |514 |
2 |515 |
1 |516 |
2 |517 |
2 |518 |
3 |519 |
3 |520 |
3 |521 |
3 |522 |
Related
I have this category which is "community-posts" I don't want it to appear on my homepage loop so I added this to my query
<?php query_posts(array('showposts' => 4,'category__not_in' => $id_communityposts,));?>
This is working fine with me but some "community-posts" I want them to be featured on the homepage loop. (exception)
so I want to only exclude the posts that has one category as "community-posts" if it has this category and more its shows normally.
First thing do not use query_posts - it should never be used as it alter the main query. Use get_posts instead - it's much safer and perform the same task.
To answer your question, let's first imagine how the query would look in SQL (assuming your $id_communityposts is equal to 2) :
SELECT DISTINCT wp_posts.*
FROM wp_posts, wp_postmeta
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)
WHERE
wp_posts.ID = wp_postmeta.post_id AND
(
(wp_term_taxonomy.taxonomy = 'category' AND wp_term_taxonomy.term_id NOT IN(2))
OR
(wp_postmeta.meta_key = 'featured' AND wp_postmeta.meta_value = 1)
)
ORDER BY wp_posts.post_date DESC
LIMIT 4
So we query the post, post meta and taxonomy tables and make two possible conditions:
The category ID is not 2, OR
The featured meta key of the post is set to 1 (change this to whatever key / value depending of how you store the "featured" information).
For that kind of specific cases, get_posts isn't really good to play with - querying the DB with WPDB will give you much more flexibility.
$posts = $wpdb->get_results(
"SELECT DISTINCT $wpdb->posts.*
FROM $wpdb->posts, $wpdb->postmeta
LEFT JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
WHERE
$wpdb->posts.ID = $wpdb->postmeta.post_id AND
(
($wpdb->term_taxonomy.taxonomy = 'category' AND $wpdb->term_taxonomy.term_id NOT IN(2))
OR
($wpdb->postmeta.meta_key = 'featured' AND $wpdb->postmeta.meta_value = 1)
)
ORDER BY $wpdb->posts.post_date DESC
LIMIT 4"
);
Let me know if you run into any issue as it is an untested query.
If I understood the question correctly , The simplest solution, not involving complicated SQL would be something along the lines of :
// NOT TESTED !
if ( count(get_the_category()) > 1 ) { // this means there are more than single category
// show the desired posts
} else {
// dont show
}
read get_the_category() in codex
Along the same logic lines you could also use wp_get_post_categories
I have some posts in my WordPress without category, Its above 2000 article, So i need to add this article into category that name is " News " by SQL
This should work:
UPDATE wp_term_relationships SET term_taxonomy_id = '3' WHERE term_taxonomy_id = '1';
The 3 is the id of the category "News", you might need to change it if it differs for you. The 1 is the category id for "Uncategorised", so the above should change all the post ids of 1 (Uncategorised) to 3 (News)
EDIT try:
UPDATE wp_term_relationships SET term_taxonomy_id = '3' WHERE term_taxonomy_id = '';
I didn't find the answer to this question, so maybe it will be helpful for someone.
you can try this code, but instead 777 - add your category id
INSERT IGNORE INTO wp_term_relationships
(object_id, term_taxonomy_id, term_order)
SELECT
wp_posts.ID as object_id,
777 as term_taxonomy_id,
0 as term_order
FROM wp_posts
LEFT JOIN
wp_term_relationships rel
ON wp_posts.ID = rel.object_id
LEFT JOIN
wp_term_taxonomy tax
ON tax.term_taxonomy_id = rel.term_taxonomy_id
AND tax.taxonomy = 'category'
LEFT JOIN
wp_terms term
ON term.term_id = tax.term_id
WHERE wp_posts.post_type = 'post'
AND wp_posts.post_status = 'publish'
AND term.term_id is null
UPDATE FROM `wp_posts` WHERE `post_category` SET `post_category` = '9' WHERE `post_category` = '';
I want to do something like that into Doctrine SQL...
SELECT N.id as n_id, C.id as c_id, T.id as t_id, C.author_id, C.body as comment FROM `news` N
LEFT JOIN thread T on T.id = N.id
LEFT JOIN comment C on C.thread_id = T.id
WHERE 1
So I have made this :
$rsm = new ResultSetMapping;
$rsm->addEntityResult('App\MyBundle\Entity\News', 'N');
$cols = array ('id', 'title','date');
foreach ($cols as $key => $col) {
$rsm->addFieldResult('N', $col, $col);
}
// thread
$rsm->addJoinedEntityResult('App\MyBundle\Entity\Thread' , 'T', 'N', 'thread');
$rsm->addFieldResult('T', 't_id', 'id');
$sql = 'SELECT N.id, N.title, N.date, T.id AS t_id FROM news N ' .
'LEFT JOIN thread T ON T.id = N.id WHERE N.id = 1';
$query = $this->_em->createNativeQuery($sql, $rsm);
But I have an error:
Notice: Undefined index: thread in /vendor/doctrine/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php line 85
I want to precise that there is no member link beetween the 2 entities.
Any ideas please?
Thanks for all
See ya
Sam
According to the docs:
The fourth and last parameter is the name of the field on the parent entity result that should contain the joined entity result.
ref: http://www.doctrine-project.org/docs/orm/2.1/en/reference/native-sql.html#joined-entity-results
you maybe missed to define the thread property in the News class that will receive your joined entity.
I have a table that saves the ratings for each post, the table looks like this http://d.pr/YIxL it saves the ID of the post and the rating, I need to query the posts based on that table ASC or DSC.
I hope that the answer is clear.
This was the query that did the trick
$wp_query->request = "
SELECT $wpdb->posts.* FROM $wpdb->posts
Left join $ratings_table
ON $wpdb->posts.ID = $ratings_table.ID
Where
$wpdb->posts.post_type = 'games' AND $wpdb->posts.post_status = 'publish'
ORDER BY $ratings_table.points DESC
LIMIT $ppp OFFSET $offset";
$pageposts = $wpdb->get_results($wp_query->request, OBJECT);
Mass delete unpopular Tags
I have 1000's of tags and I would like to simply delete all tags that aren't used more than X times... i.e. 5 times.
Does anyone know of a simple way to do this? Even straight SQL would totally ROCK!
Doing with with an SQL command is going to be your best bet. The tables wp_terms, wp_term_relationships, and wp_term_taxonomy are the ones of interest. WordPress keeps track of relationships with wp_term_taxonomy.count. So this help make life easier.
-- remove terms
DELETE FROM wp_terms WHERE term_id IN (SELECT term_id FROM wp_term_taxonomy WHERE taxonomy = 'tag' AND count <= 5;)
-- remove all relationships
DELETE FROM wp_term_relationships WHERE term_taxonomy_id IN (SELECT term_taxonomy_id FROM wp_term_taxonomy WHERE taxonomy = 'tag' AND count <= 5;)
-- remove taxonomy entry
DELETE FROM wp_term_taxonomy WHERE taxonomy = 'tag' AND count <= 5;
Note: I would strongly suggest making a backup of these tables before running the commands above.
Make sure to back up the database first
DELETE a,c FROM wp_terms AS a
LEFT JOIN wp_term_taxonomy AS c ON a.term_id = c.term_id
LEFT JOIN wp_term_relationships AS b on b.term_taxonomy_id = c.term_taxonomy_id
WHERE ( c.taxonomy = 'post_tag' AND c.count <= 5 );
On the last line, you could also modify c.count <= 5 and replace 5 with any number of your choice.
Jasons answer worked except in my install, the name of the taxonomy was 'post_tag' not 'tag'
$x = 5; // set this to any number
$sql = "SELECT `name` FROM `wp_terms`";
$result = mysql_query($sql);
$count = array();
while($row = mysql_fetch_assoc($result))
{
$count[$name]++;
}
foreach($count as $key = $value)
{
if($value < $x)
{
$sql2 = "DELETE FROM `wp_terms` WHERE `name` = '". $key ."'";
$result2 = mysql_query($sql2);
}
}
There's more efficient ways of doing it but this will do the job.
Edit: make a backup first. I'm not entirely sure that that table is exclusive to tags.