Forum Replies Created

Viewing 15 replies - 16 through 30 (of 132 total)
  • Thread Starter imjscn

    (@imjscn)

    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.

    Thread Starter imjscn

    (@imjscn)

    @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!

    Thread Starter imjscn

    (@imjscn)

    I see.
    Thanks for helping! I will try to see if I can apply this code on postmeta table or not.

    Thread Starter imjscn

    (@imjscn)

    @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;

    Thread Starter imjscn

    (@imjscn)

    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_id

    The 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!

    Thread Starter imjscn

    (@imjscn)

    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 to

    SELECT  project_id.post_id, project_id.meta_value,project_id.meta_key

    got 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.

    Thread Starter imjscn

    (@imjscn)

    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 width

    var_dump returns array(0) { }
    (if don’t add height and width table in FROM line, the same 0 return )

    Please give further instruction!

    Thread Starter imjscn

    (@imjscn)

    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?

    Thread Starter imjscn

    (@imjscn)

    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)

    Thread Starter imjscn

    (@imjscn)

    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?

    Thread Starter imjscn

    (@imjscn)

    Great! your code works!
    Now I got the private variable’s value connected to its original name.
    Thanks a lot!

    Thread Starter imjscn

    (@imjscn)

    I tried

    $item_a = $project['height'];
    echo $item_a;

    it outputs nothing.

    Thread Starter imjscn

    (@imjscn)

    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 =>yyy

    Now I need to say:
    $item_a = xxx;
    $item_b = yyy;

    How?
    Thanks!

    Thread Starter imjscn

    (@imjscn)

    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'];
    }

    Thread Starter imjscn

    (@imjscn)

    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!

Viewing 15 replies - 16 through 30 (of 132 total)