Support » Fixing WordPress » getting array into query_posts()…

  • glennn

    (@glennn)


    having this query:

    $query = "Select MLS_Number from idx WHERE City = 'ALAMO'";
    $result1 = Mysql_query($query) or die("<font color=\"#FF0000\">Query Error</font>" . mysql_error());
    while($row = mysql_fetch_array($result1, MYSQL_ASSOC))
    {
    $MLS_Number = $row['MLS_Number'];
    $query2 = "select post_id from wp_postmeta where meta_key = '_option_8' and meta_value = '$MLS_Number'";
    $result2 = Mysql_query($query2) or die("<font color=\"#FF0000\">Query Error</font>" . mysql_error()); 
    
    	while($row = mysql_fetch_array($result2, MYSQL_ASSOC))
    	{
    	$Post_id = $row['post_id'];
    	$Uquery = "update wp_term_relationships set term_taxonomy_id = 10 where term_taxonomy_id = 11 and object_id = $Post_id";
    	$result = Mysql_query($Uquery) or die("<font color=\"#FF0000\">Query Error</font>" . mysql_error()); 
    
    // echo  $MLS_Number." -- ";
    // echo $Post_id." | ";
    
    	}
    }

    i’ll retrieve 2 listings, for instance post #s 21 and 30… (conceivably could return many)

    the value of $Post_id would be “2130”…

    no matter what i do, i can’t get the array into query_posts(); to run the loop.

    can someone please help? i’m not good with arrays, implode etc, anyway.

    thanks so much!

    G

Viewing 6 replies - 1 through 6 (of 6 total)
  • MichaelH

    (@michaelh)

    Seems to me that $Post_id would only be one value at a time like 21, but not 2130.

    $args=array(
      'post__in'=>array($Post_id),
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().

    glennn

    (@glennn)

    i take that back – the $MLS_Number returned 123456123456 (where a single mls # is 123456) – when i echod “$mls#” and then “$post_id” i got them both, as long as i stayed within either while() {…

    heck, i dunno.
    i’ll try yours – i played with the $args=array( for a bit, but couldn’t get what i wanted. i was just using ‘post__in’ => array($Post_ID) – and i was getting an error, unexpected double arrow or something…

    i’ll try this and let you know. thanks

    glennn

    (@glennn)

    ok, for sure, help.

    $query = "Select MLS_Number from idx WHERE City = 'ALAMO'";
    $result1 = Mysql_query($query) or die("<font color=\"#FF0000\">Query Error</font>" . mysql_error());
    while($row = mysql_fetch_array($result1, MYSQL_ASSOC))
    {
    $MLS_Number = $row['MLS_Number'];
    
    $query2 = "select post_id from wp_postmeta where meta_key = '_option_8' and meta_value = '$MLS_Number'";
    $result2 = Mysql_query($query2) or die("<font color=\"#FF0000\">Query Error</font>" . mysql_error()); 
    
    	while($row = mysql_fetch_array($result2, MYSQL_ASSOC))
    	{
    	$Post_id = $row['post_id'];
    	$Uquery = "update wp_term_relationships set term_taxonomy_id = 10 where term_taxonomy_id = 11 and object_id = $Post_id";
    	$result = Mysql_query($Uquery) or die("<font color=\"#FF0000\">Query Error</font>" . mysql_error());
    	// echo  $MLS_Number." -- ";
    	// echo $Post_id." | ";
    
    echo $Post_id;
    
    	}
    }

    this does indeed return “2130”…

    your array only returns the last post entry, 30. 21 and 30 ARE both posts and published. nothing different about them.

    PLEASE HELP????

    glennn

    (@glennn)

    this semi worked:

    $query = "Select MLS_Number from idx WHERE City = 'ALAMO'";
    $result1 = Mysql_query($query) or die("<font color=\"#FF0000\">Query Error</font>" . mysql_error());
    while($row = mysql_fetch_array($result1, MYSQL_ASSOC))
    {
    $MLS_Number = $row['MLS_Number'];
    
    $query2 = "select post_id from wp_postmeta where meta_key = '_option_8' and meta_value = '$MLS_Number'";
    $result2 = Mysql_query($query2) or die("<font color=\"#FF0000\">Query Error</font>" . mysql_error()); 
    
    	while($row = mysql_fetch_array($result2, MYSQL_ASSOC))
    	{
    	$Post_id = $row['post_id'];
    	$Uquery = "update wp_term_relationships set term_taxonomy_id = 10 where term_taxonomy_id = 11 and object_id = $Post_id";
    	$result = Mysql_query($Uquery) or die("<font color=\"#FF0000\">Query Error</font>" . mysql_error());
    	// echo  $MLS_Number." -- ";
    	// echo $Post_id." | ";
    	}
    
    echo $Post_id;
    $args=array(
      'post__in'=>array($Post_id),
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    
    if( $my_query->have_posts() ) {
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php
      endwhile;
    }
    
    }

    but it printed “

    21
    Post Tile

    30
    Post Title “

    which tells me it’s running the query_posts for each $Post_ID, which is not good…?

    that would tell a real WP person what’s going on, tho…? :o)

    glennn

    (@glennn)

    i’m guessing maybe it’s ok that WP_Query($args) runs once for EACH post that’s returned? if there’re 100 results, will this be ok?

    thanks!!!

    MichaelH

    (@michaelh)

    which tells me it’s running the query_posts for each $Post_ID, which is not good…?

    If you don’t like that, then collect each $Post_id into an array, then after getting all that values into that array, use that array with query_posts.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘getting array into query_posts()…’ is closed to new replies.