Home > Back-end >  Getting post ids from WordPress taxonomy
Getting post ids from WordPress taxonomy

Time:11-04

I need to retrieve first and second post ID of current taxonomy page.

So for example when I'm on page mywebsite.com/tag/Gaming-laptop I have lists of laptops with tag "gaming laptop" i do need to get post ID of first and second laptop that was displayed by current taxonomy.

I was looking in wordpress documentation but I can't find related function

CodePudding user response:

Just some code to get started. This will get you all the IDs for job_listings that are assigned to term 4 in your taxonomy.

 <?php
$posts = get_posts( array(
    'posts_per_page' => -1,

    'fields' => 'ids',

    'post_type' => 'job_listing',

    'tax_query' => array(

        array(

            'taxonomy' => 'agency',

            'field' => 'term_id',

            'terms' => 4
        )
    )
) );
echo implode( ', ', $posts ); ?>

CodePudding user response:

if you are on taxonomy page then use below code. replace taxonomy name.

<?php
$term_id = get_queried_object()->term_id;
$args = array(
'post_type' => 'recipe_cpt',
'post_status' => 'publish',
'posts_per_page' => 2,
'tax_query' => array(
    array(
    'taxonomy' => 'recipe_tx',
    'field' => 'term_id',
    'terms' => $term_id
     )
  )
);
$query = new WP_Query( $args ); ?>
  • Related