The MU forums have moved to WordPress.org

How to query wp_x_post2cat table with a normal mysql command (4 posts)

  1. jeepmac
    Member
    Posted 16 years ago #

    There is no $wpdb->get_col command to query post2cat in my CRON php file. So I use a normal sql command instead:

    $sql = "select post_id from wp_".$category[blog_id]."_post2cat where category_id=$category[cat_ID]";
    $posts = mysql_query($sql);

    But the result only contains one row. That's means if the table wp_x_post2cat has many post_id correspond to the category_id, the query only outputs one post_id.

    Any help is appreciated...

  2. drmike
    Member
    Posted 16 years ago #

    Not sure where you're getting $category[blog_id] from. Can you post the entire script up somewhere like pastebin or a txt file so we can see it please?

    $blog_id's a global so it should just be $blog_id. have you tried that?

  3. jeepmac
    Member
    Posted 16 years ago #

    Hi Dr. Mike,

    This CRON file is just 'latestposts.php', written by ColinFilmJournal.

    I want to add site-wide category search on it:

    
    // site wide Category Add by jeepmac
    	$sql = "select blog_id, path from wp_blogs where public=1 and spam=0 and blog_id>1";
    	$blogs = mysql_query($sql);
    	$sql = "";
    	$i = 0;
    	while ($blog = mysql_fetch_array($blogs)) {
    		if ($i == 0) {
    			$sql = $sql."select *, $blog[blog_id] as blog_id from wp_".$blog[blog_id]."_categories where category_nicename != 'uncategorized' and  category_nicename != 'blogroll' and  category_count !=0 ";
    		} else {
    			$sql = $sql."UNION select *, $blog[blog_id] as blog_id from wp_".$blog[blog_id]."_categories  where category_nicename != 'uncategorized' and  category_nicename != 'blogroll' and  category_count !=0 ";
    		}
    		$i++;
    	}
    
    	$categories = mysql_query($sql);
    	mysql_query("delete from latest_categories");
    	mysql_query("delete from latest_post2cat");
    
    	while ($category = mysql_fetch_array($categories)) {
    
    		$sql = "select * from latest_categories where category_nicename='$category[category_nicename]'";
    		$temp = mysql_query($sql);
    		$temp = mysql_fetch_array($temp);
    		if ( $temp[category_count] ) {
    			$sql = "UPDATE latest_categories SET category_count =category_count+$category[category_count] WHERE category_nicename='$category[category_nicename]'";
    			mysql_query($sql);
    		} else {
    			$sql = "insert into latest_categories (cat_name, category_nicename, category_description, category_parent, category_count, link_count, posts_private, links_private) values ('$category[cat_name]', '$category[category_nicename]', '$category[category_description]', '$category[category_parent]', $category[category_count], $category[link_count], $category[posts_private], $category[links_private]);";
    			mysql_query($sql);
    		}
    
    		$sql = "select post_id from wp_".$category[blog_id]."_post2cat where category_id=$category[cat_ID]";
    		$posts = mysql_query($sql);
    
    		while ($post = mysql_fetch_array($posts)) {
    			$sql = "insert into latest_post2cat (blog_id, post_id, category_nicename) values ($category[blog_id], $post[post_id], '$category[category_nicename]')";
    			mysql_query($sql);
    		}
    	}
    

    I create two tables for this function: latest_categories and latest_post2cat. The code can get wide site category successfully into ‘latest_categories'. But for every category, only one post_id is inserted to latest_post2cat.

  4. jeepmac
    Member
    Posted 16 years ago #

    I added 'echo count($posts);' after:

    
    $sql = "select post_id from wp_".$category[blog_id]."_post2cat where category_id=$category[cat_ID]";
    $posts = mysql_query($sql);
    

    it always show '1' for each category_id.

About this Topic