Запросы к базе данных Wordpress

1. Выборка одной записи-поста с указанием автора, категории и двух именованных метаданных

SELECT
    wp_posts.ID,
    wp_posts.post_title,
    wp_posts.post_name,
    wp_posts.post_date,
    wp_users.user_nicename,
    wp_terms.name AS cat_name,
    wp_terms.slug AS cat_slug,
    wp_postmeta.meta_value AS ogimg,
(
    SELECT wp_postmeta.meta_value 
    FROM wp_postmeta 
    WHERE wp_postmeta.meta_key = 'from_here' 
        AND wp_postmeta.post_id = wp_posts.ID) AS from_here
FROM wp_posts
LEFT JOIN wp_users ON wp_posts.post_author = wp_users.ID
LEFT JOIN wp_term_relationships 
                ON wp_posts.ID = wp_term_relationships.object_id
LEFT JOIN wp_term_taxonomy USING (term_taxonomy_id)
LEFT JOIN wp_terms USING (term_id)
LEFT JOIN wp_postmeta ON wp_posts.ID = wp_postmeta.post_id
WHERE wp_posts.post_name = 'link-to-article'
    AND wp_term_taxonomy.taxonomy = 'category'
    AND wp_postmeta.meta_key = 'ogimg';

2. Выборка всех записей блога принадлежащих одной метке (с анонсом в 30 слов)

SELECT DISTINCT
    wp_terms.name, wp_terms.slug, 
    wp_posts.post_title, wp_posts.post_name, 
    SUBSTRING_INDEX(wp_posts.post_content, ' ', 30) AS post_content
FROM wp_terms
LEFT JOIN wp_term_taxonomy USING (term_id)
LEFT JOIN wp_term_relationships USING (term_taxonomy_id)
LEFT JOIN wp_posts ON wp_term_relationships.object_id = wp_posts.ID
WHERE wp_terms.slug = 'holmogorov'

3. Выборка родительских страниц с указанием количества дочерних

SELECT
COUNT(wp1.post_parent) as cou,
    wp2.ID,
    wp2.post_name,
    wp2.post_title
FROM wp_posts as wp1
LEFT JOIN wp_posts as wp2 ON wp1.post_parent = wp2.ID
WHERE wp1.post_parent != 0 
    AND wp1.post_status = 'publish' 
    AND wp1.post_type = 'page'
GROUP BY wp1.post_parent
HAVING cou > 1
ORDER BY cou DESC

4. Все записи одной категории с анонсом из 30 слов

SELECT DISTINCT     
    wp_posts.post_title, wp_posts.post_name,
    wp_posts.post_date, wp_users.user_nicename,
    SUBSTRING_INDEX(wp_posts.post_content, ' ', 30) AS post_content,
    wp_terms.name, wp_terms.slug
FROM wp_terms
LEFT JOIN wp_term_taxonomy USING (term_id)
LEFT JOIN wp_term_relationships USING (term_taxonomy_id)
LEFT JOIN wp_posts ON wp_term_relationships.object_id = wp_posts.ID
LEFT JOIN wp_users ON wp_users.ID = wp_posts.post_author
WHERE wp_terms.slug = 'articles' 
    AND wp_posts.post_status = 'publish' 
ORDER BY post_date DESC

5. Выборка всех записей одного автора с указанием рубрики блога

SELECT
    wp_posts.post_title,
    wp_posts.post_name,
    wp_posts.post_date,
    wp_terms.name as cat_name,
    wp_terms.slug as cat_slug,
    wp_users.user_nicename
FROM wp_posts
LEFT JOIN wp_users ON wp_users.ID = wp_posts.post_author
LEFT JOIN wp_term_relationships 
            ON wp_term_relationships.object_id = wp_posts.ID
LEFT JOIN wp_term_taxonomy USING (term_taxonomy_id)
LEFT JOIN wp_terms USING (term_id)
WHERE wp_users.user_nicename = 'styleroom'
    AND wp_term_taxonomy.taxonomy = 'category'
    AND wp_posts.post_status = 'publish'
GROUP BY post_title
ORDER BY wp_posts.post_date DESC

еще запись