简体   繁体   中英

Wordpress: Filter category on multisite posts query

My Situation

First of all, I haven't made any custom SQL querys before so bear with me..

I have a multisite installation with several blogs. The following function querys the latest posts from all blogs and that works fine.

Working Code, taken from https://gist.github.com/mhulse/5718743

function recent_ms_posts($count = 10, $cat, $ignore = array('')) {

    global $wpdb, $table_prefix;

    $ignore = implode(', ', array_map('absint', $ignore));
    $rows = NULL;
    $tables = array();
    $query = '';
    $i = 0;
    $posts = array();
    $post = NULL;

    $rows = $wpdb->get_results($wpdb->prepare("SELECT blog_id FROM $wpdb->blogs WHERE blog_id NOT IN ($ignore) AND public = '1' AND archived = '0' AND mature = '0' AND spam = '0' AND deleted = '0'", 0), ARRAY_A);

    if ($rows) {

        foreach ($rows as $row) {
            $tables[$row['blog_id']] = $wpdb->get_blog_prefix($row['blog_id']) . 'posts';
        }

        if (count($tables)) {
            foreach ($tables as $blog_id => $table) {
                if ($i)
                    $query .= ' UNION ';

                $query .= " (SELECT ID, post_date, $blog_id as `blog_id` FROM $table WHERE post_status = 'publish' AND post_type = 'post')";
                $i++;
            }

            $query .= " ORDER BY post_date DESC LIMIT 0, $count;";
            $rows = $wpdb->get_results($query);

            if ($rows) {
                foreach ($rows as $row) {
                    $post = get_blog_post($row->blog_id, $row->ID);
                    $post->blog_id = $row->blog_id;
                    $post->row_id =$row->ID;
                    $post->permalink = get_blog_permalink($row->blog_id, $row->ID);
                    $posts[] = $post;
                }

                return $posts;
            }
        }
    }
}

My Problem

What I now want to do is to filter out posts from a certain category and I tried modifying the query string a bit:

Modified, not working code

$query .= " (SELECT ID, post_date, $blog_id as `blog_id` FROM $table WHERE post_status = 'publish' AND post_type = 'post' AND term_taxonomy.taxonomy = 'category' AND terms.slug = '$cat')";

My Question

How can I make the term_taxonomy.taxonomy = 'category' and terms.slug = '$cat' work as I want them to?

Okay, so I solved the problem with by altering the $query string and adding some variables:

$table_posts = $table . 'posts';
$table_term_relationships = $table . 'term_relationships';
$table_term_taxonomy = $table . 'term_taxonomy';
$table_terms = $table . 'terms';

if ($i)
    $query .= ' UNION ';

$query .= " (
    SELECT 
    $table_posts.ID, 
    $table_posts.post_date, 
    $blog_id as `blog_id` 
    FROM 
    $table_posts 
    LEFT JOIN $table_term_relationships ON ($table_posts.ID = $table_term_relationships.object_id) 
    LEFT JOIN $table_term_taxonomy ON ($table_term_relationships.term_taxonomy_id = $table_term_taxonomy.term_taxonomy_id) 
    LEFT JOIN $table_terms ON ($table_term_relationships.term_taxonomy_id = $table_terms.term_id) 
    WHERE 
    post_status = 'publish' 
    AND 
    post_type = 'post'
    AND
    $table_term_taxonomy.taxonomy = 'category' 
    AND
    $table_terms.slug LIKE '$cat'
    )";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM