imjscn
Forum Replies Created
-
Forum: Developing with WordPress
In reply to: How to run a left join query on postmeta table?Great! Got all projects by $data[] = …
Still need one more tip to complete-
Now, each project is an array, how to get it as object(stdClass){…} ?The array comes from this:
$data[] = Array(...);
If I change to $data[] = object(…), then , the “=>” is wrong.Forum: Developing with WordPress
In reply to: How to run a left join query on postmeta table?@apljdi
Seems there’s no way to query colums as keys in postmeta table. I decide to move on in other ways.Somedays ago, I learned get_post_meta from you. Now I do it this way:
function mp_all_ids( ){ global $wpdb, $post; $query = <<<QUERY SELECT project_id.post_id FROM $wpdb->postmeta project_id WHERE project_id.meta_key = 'project_id' ORDER BY project_id.meta_value+(0) ASC QUERY; $postids = $wpdb->get_col( $query ); foreach ($postids as $id) { $meta=get_post_custom($id); $data = Array( 'ID' => $meta['project_id'][0], 'height' => $meta['height'][0], 'width' => $meta['width'][0] ); } return apply_filters( 'mp_all_ids', $data ); }From var_dump($data), this function get only one result(because of Foreach) like this:
array(3) { ["ID"]=> string(3) "463" ["height"]=> string(16) "100" ["width"]=> string(32) "50" }How can I get all projects with data like this:
array(2) { [0]=> object(stdClass)#1 (3) { ["ID"]=> string(1) "2" ["height"]=> string(3) "100" ["width"]=> string(2) "50" } [1]=> object(stdClass)#2 (3) { ["ID"]=> string(1) "1" ["height"]=> string(3) "200" ["width"]=> string(3) "100" } }Please help!
Forum: Developing with WordPress
In reply to: How to run a left join query on postmeta table?I see.
Thanks for helping! I will try to see if I can apply this code on postmeta table or not.Forum: Developing with WordPress
In reply to: How to run a left join query on postmeta table?@apljdi,
Could you help check this code? It gets the correct result but from multi tables, I don’t understand what is pd, pa, and u. / p. , hope there’s a way I can borrow this code to apply on postmeta table.$query = <<<QUERY SELECT DISTINCT(u.ID), u.user_email, u.user_nicename, u.display_name, pd.value FROM $wpdb->users u LEFT JOIN {$bp->profile->table_name_data} pd ON u.ID = pd.user_id LEFT JOIN ( SELECT DISTINCT( p.post_author ) ID FROM $wpdb->posts p WHERE p.post_type = 'ep_reg' AND p.post_parent = $postid $for_admin ) pa ON u.ID = pa.ID WHERE u.user_status = 0 AND pa.ID IS NULL AND pd.field_id = 1 ORDER BY pd.value ASC QUERY;Forum: Developing with WordPress
In reply to: How to run a left join query on postmeta table?New finding- if I change the first 2 lines to:
SELECT project_id.post_id, height.meta_value,width.meta_value FROM $wpdb->postmeta project_idThe output will show:
array(3) { [0]=> object(stdClass)#101 (3) { ["post_id"]=> string(3) "1" ["meta_value"]=> string(3) "50" } [1]=>{...} [2]=>{...} }After serveral test, I see it can only output 3 items in one object (post_id, meta_key, meta_value)
It takes whatever value (height,width…)I specific in SELECT line, but each object can only have one post_id, one meta_key and one meta_value.
So, now the problem is a bit clear– my code makes the query result structured the same as postmeta table, to get one id with all related height/width…, I need another structure.
Please help!Forum: Developing with WordPress
In reply to: How to run a left join query on postmeta table?I put the 2 lines in template file, right below the line I echo the query result, it prints nothing.
<?php $wpdb->show_errors(); ?> <?php $wpdb->print_error(); ?>I changed the 2nd line of the query to FROM $wpdb->postmeta project_id, still no error messages and returns project_id, This line might be the problem, coz it’s the project_id, not contains height and width info.
Here’s how they look like in postmeta table:------------------------------------- post_id | meta_key | meta_value | ------------------------------------- 1 | project_id | 1 | ------------------------------------- 1 | height | 50 | ------------------------------------- 1 | width | 100 | ------------------------------------- 2 | project_id | 2 | ------------------------------------- 2 | height | 50 | ------------------------------------- 2 | width | 100 | --------------------------------------Edit:
Now I changed the 1st line toSELECT project_id.post_id, project_id.meta_value,project_id.meta_keygot this:
array(3) { [0]=> object(stdClass)#101 (3) { ["post_id"]=> string(3) "1" ["meta_value"]=> string(3) "1" ["meta_key"]=> string(7) "project_id" } [1]=>{...} [2]=>{...} }Seems, the “FROM $wpdb->postmeta project_id ” is wrong.
But, “FROM $wpdb->postmeta” deosn’t give any result.Forum: Developing with WordPress
In reply to: How to run a left join query on postmeta table?ok,just now I changed the first 2 lines to:
SELECT project_id.post_id, height.height, width.width FROM $wpdb->postmeta project_id,$wpdb->postmeta height,$wpdb->postmeta widthvar_dump returns array(0) { }
(if don’t add height and width table in FROM line, the same 0 return )Please give further instruction!
Forum: Developing with WordPress
In reply to: what's the output of this query?get_post_meta only get one post’s meta, and I need all posts’ metas.
query_posts will get all the posts but that’s not what I want. I don’t want the posts, I want to loop the object out of post loop.
Any suggestion?Forum: Developing with WordPress
In reply to: what's the output of this query?I searched out an example, this is exactly the output formate I need:
array(2) { [0]=> object(stdClass)#1 (3) { ["ID"]=> string(1) "2" ["name"]=> string(18) "cat" ["desc"]=> string(4) "cc" } [1]=> object(stdClass)#2 (3) { ["ID"]=> string(1) "1" ["name"]=> string(19) "dog" ["desc"]=> string(7) "dd" } }but the output was generated by a query that run on multi-table. Please help me figure out a query to run on wp postmeta table( one table only)
Forum: Developing with WordPress
In reply to: what's the output of this query?I tried a var_dump, got this:
array(3) { [0]=> object(stdClass)#457 (1) { ["post_id"]=> string(3) "385" } [1]=> object(stdClass)#458 (1) { ["post_id"]=> string(3) "451" } [2]=> object(stdClass)#377 (1) { ["post_id"]=> string(3) "453" } }Now, how can I get the 3 strings in each object?
Forum: Fixing WordPress
In reply to: add_action / do_action not working in a class ?Great! your code works!
Now I got the private variable’s value connected to its original name.
Thanks a lot!Forum: Fixing WordPress
In reply to: add_action / do_action not working in a class ?I tried
$item_a = $project['height']; echo $item_a;it outputs nothing.
Forum: Fixing WordPress
In reply to: add_action / do_action not working in a class ?Seems the ‘width’ and ‘height’ is an anrray in itself, can print the value but the key is always 0. So, I changed to do like this:
$boxes = Array ( 'width', 'height'); $fields = get_post_custom( $post->ID); foreach ( $boxes as $data ){ $project = $fields[$data]; foreach ( $project as $k => $v ) echo $data . " => " . $v . "<br />"; }This gets the output of
width => xxx
height =>yyyNow I need to say:
$item_a = xxx;
$item_b = yyy;How?
Thanks!Forum: Fixing WordPress
In reply to: add_action / do_action not working in a class ?Thanks again!
I will try the get_post_custom. Is this correct?–$fields = get_post_custom( $this->id); if ( !empty( $fields['width'] ) ){ $this->item_a = $fields['width']; $this->item_b = $fields['height']; }Forum: Fixing WordPress
In reply to: add_action / do_action not working in a class ?Thanks! I tried your solution, it works– saved the group of data into post meata, including those private variables under other name.
Now, in the class, when the project is not new, I need to obtain the meta data and assign the value to each variable–
$boxes = Array( 'height', 'width' ); foreach ( $boxes as $data ){ $project = get_post_meta($post->ID, $data, true); } $item_a = $project->height; $item_b = $project->width;I tried var_dump, can’t get $item_a and $item_b.
Please help!