• Resolved juzkyte

    (@juzkyte)


    I want to get other post from tag in post

    In a post can have one, two,.. or more tags…

    I want to get query post from tag, example: tag_id=1,2,3…

    ?? Help me…

Viewing 13 replies - 1 through 13 (of 13 total)
  • // WP_Query arguments
    $args = array ( 'tag_id' => '1,2,3' );
    // The Query
    $query = new WP_Query( $args );

    OR

    // WP_Query arguments
    $args = array ( 'tag_name' => 'blogging, wordpress, slug');
    // The Query
    $query = new WP_Query( $args );
    Thread Starter juzkyte

    (@juzkyte)

    `<?php
    // the query $args = array ( ‘tag_id’ => ‘1,2,720’ );
    $the_query = new WP_Query( $args ); ?>

    <?php if ( $the_query->have_posts() ) : ?>

    <!– pagination here –>

    <!– the loop –>
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    <h2><?php the_title(); ?></h2>
    <?php endwhile; ?>
    <!– end of the loop –>

    <!– pagination here –>

    <?php wp_reset_postdata(); ?>

    <?php else: ?>
    <p><?php _e( ‘Sorry, no posts matched your criteria.’ ); ?></p>
    <?php endif; ?>

    I used this code but not work…

    Moderator bcworkz

    (@bcworkz)

    Try this variant:

    <?php
    // the query
    $args = array('tag__in' => array( 1, 2, 720,),);
    $the_query = new WP_Query( $args ); ?>

    Be sure every line is really a separate line as shown, sometimes a missing line feed is not apparent but throws off the parser.

    Thread Starter juzkyte

    (@juzkyte)

    Thanks bcworkz!

    And I want to get list tag id of post separated by commas? how?

    Thread Starter juzkyte

    (@juzkyte)

    Oh…

    <?php $posttags = get_the_tags(); if ($posttags) {foreach($posttags as $tag) {echo $tag->term_id.',';}}?>

    Thanks for your support

    Moderator bcworkz

    (@bcworkz)

    Don’t you then end up with a dangling comma at the end of the list? An improvement (IMHO) would be to use the loop to collect the IDs into an array, then use join() to build the list with no dangling comma, which is then echoed out all at once.

    Thread Starter juzkyte

    (@juzkyte)

    I don’t know… you can tell me more detail?

    Moderator bcworkz

    (@bcworkz)

    Your code outputs something like this:
    21,33,45,123,

    with the comma on the end. Maybe that works with how you are displaying the IDs, or it just doesn’t matter to you. Little details like that bother some people, sometimes including me. I would prefer the output is more like this:
    21, 33, 45, 123

    Not a huge difference, but it matters to some, and the additional coding is minimal, like so:

    $posttags = get_the_tags();
    $tag_ids = array();
    if ( $posttags ) {
       foreach( $posttags as $tag ) {
          $tag_ids[] = $tag->term_id;
       }
    }
    echo join( ', ', $tag_ids );

    Thread Starter juzkyte

    (@juzkyte)

    Please check your code? This not work exactly ??

    Moderator bcworkz

    (@bcworkz)

    ???
    Not work exactly how?
    Works for me, on a template in the “Loop”, exactly as I expected:
    21, 33, 45, 123

    Thread Starter juzkyte

    (@juzkyte)

    <?php
    $posttags = get_the_tags();
    if ($posttags) {$dem= 0; foreach($posttags as $tag) {$dstag[$dem]= $tag->term_id; $dem++;}}
    $args = array('tag__in' => $dstag);
    $the_query = new WP_Query( $args );  ?>
    
    <?php if ( $the_query->have_posts() ) : ?>
    
    <!-- pagination here -->
    
    	<!-- the loop -->
    	<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    	<h2><?php the_title(); ?></h2>
    	<?php endwhile; ?>
    	<!-- end of the loop -->
    
    <!-- pagination here -->
    
    <?php wp_reset_postdata(); ?>
    
    <?php else: ?>
    	<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
    <?php endif; ?>

    I used this code, worked but…
    This code will show posts from tags of current post… but, it show current post… Please help me except current post ??

    Moderator bcworkz

    (@bcworkz)

    Use this for the WP_Query args:

    $args = array(
       'tag__in' => $dstag,
       'post__not_in' => array( get_the_ID()),
    );

    Thread Starter juzkyte

    (@juzkyte)

    Thanks you!

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Get query post form tag id in post’ is closed to new replies.