• Hi!
    I would like to get all IDs from a custom post type (hoteis) that have in term from a taxonomy (localhotel). I use this in taxonomy-localhotel.php template.

    I put just a piece of code, because the problem is to make $wpdb getting from multiple tables:

    $termoatual = $wp_query->queried_object->term_id;
    $sticky_IDs = $wpdb->get_col(“SELECT ID from “.$wpdb->prefix.”posts WHERE post_type = ‘hoteis'”);
    $sticky_posts = array_intersect(get_option(‘sticky_posts’),$sticky_IDs);

    In this part:
    $sticky_IDs = $wpdb->get_col(“SELECT ID from “.$wpdb->prefix.”posts WHERE post_type = ‘hoteis'”);

    i need to get post type hoteis but only with taxonomy localhotel and the term that is actual viewed by template. ($termoatual = $wp_query->queried_object->term_id;)

Viewing 7 replies - 1 through 7 (of 7 total)
  • I’m pretty sure you can use the get_posts() function to do what you’re trying to do. I’ll post an example of the code below.

    <?php
    $args = array(
    	 'posts_per_page' => -1,
    	 'orderby' => 'ASC',
    	 'post_type' => 'Hotels',
    	 'localhogtel' => 'YOUR TAXONOMY TERM',
    	 'post_status' => 'publish'
    );
    $post_ids = get_posts( $args );
    ?>

    This returns an array of posts from which you can pull the appropriate metadata with the following code:

    $post_ids[0]->ID;

    If you needed to, you could loop through the whole array, it should do what you’re describing. If i’m understanding what you want properly.

    Thread Starter driano

    (@driano)

    Hi!! Thanks for the support! I modify the code:

    $args = array(
    			 'posts_per_page' => -1,
    			 'orderby' => 'ASC',
    			 'post_type' => 'Hoteis',
    			 'localhotel' => 'juiz de fora',
    			 'post_status' => 'publish',
    			 'post__in'=> get_option( 'sticky_posts' ),
    		);
    		$post_ids = get_posts( $args );

    [Moderator Note: Please post code & markup between backticks or use the code button. Your posted code may now have been permanently damaged by the forum’s parser.]

    that works, but i will need to make a loop to get all the ids…

    And if i use $wpdb->get_col, im get the ids using this:

    $sticky_IDs = $wpdb->get_col(“SELECT ID from “.$wpdb->prefix.”posts WHERE post_type = ‘hoteis'”);
    $sticky_posts = array_intersect(get_option(‘sticky_posts’),$sticky_IDs);

    You know how make the query works using $wpdb->get_col? I know that i need to combine other table with wp_posts, but i dont know how, if is inner join…

    I would have thought that the code above would have yielded an array of post objects that contained all the IDs that fell into the parameters you set forth. What is there left to do? What are you trying to do with the IDs?

    Thread Starter driano

    (@driano)

    I need to get all ids from posts in these conditions: custom post type + taxonomy term + sticky and return in $sticky_posts

    The code is wright but i will need to do a loop to get all ids and save in array. To use in my next query for example that have:

    ‘post__in’=> $sticky_posts
    or
    ‘post__not_in’=>$sticky_posts

    The code:
    $sticky_IDs = $wpdb->get_col(“SELECT ID from “.$wpdb->prefix.”posts WHERE post_type = ‘hoteis'”); (need a modification)
    $sticky_posts = array_intersect(get_option(‘sticky_posts’),$sticky_IDs);

    get me the ids without make a loop… but i can use the code that you make and a loop to return…

    but thats ok ??

    I see, no wonder you wanted to use an SQL query. I’m pretty sure the following code will give you that properly ordered array of sticky post ids. Sorry, I’d offer suggestions on how to modify your SQL query, but i’m not the best at it.

    $args = array(
    			 'posts_per_page' => -1,
    			 'orderby' => 'ASC',
    			 'post_type' => 'Hoteis',
    			 'localhotel' => 'juiz de fora',
    			 'post_status' => 'publish',
    			 'post__in'=> get_option( 'sticky_posts' ),
    		);
    		$post_ids = get_posts( $args );
                    $stickyids = array();
    
    foreach ($post_ids as $ids) {
    global $stickyids;
    $i = $ids->ID;
    array_push($i, $stickyids);
    }
    array_reverse($stickyids);
    Thread Starter driano

    (@driano)

    Thanks for all!! ?? i think it will be work!

    Glad to help

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘$wpdb->get_col with multiple tables’ is closed to new replies.