Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #1442876
    Manel
    Guest

    Hi Team,
    I need to do a query from PORTFOLIO items.
    I want to show 1 PORTFOLIO ID for each different CATEGORY attached in related_categories field.

    My code is this, but I have a problem in TAXONOMI_NAME… Wich is?


    // Get the related categories
    $related_categories = get_field('related_categories');

    // Initialize an array to store random IDs per category
    $random_ids_per_category = array();

    // Loop through the related categories
    foreach ($related_categories as $category) {
    // Perform a query to get the related IDs for the current category
    $query = new WP_Query(
    array(
    'post_type' => 'portfolio',
    'posts_per_page' => -1,
    'tax_query' => array(
    array(
    'taxonomy' => 'taxonomy_name', // Replace 'taxonomy_name' with the name of your taxonomy
    'field' => 'term_id',
    'terms' => $category->term_id,
    ),
    ),
    'fields' => 'ids', // Get only the post IDs
    )
    );

    // Get the post IDs found
    $post_ids = $query->posts;

    // Select a random post ID from the found IDs
    $random_post_id = $post_ids[array_rand($post_ids)];

    // Store the random ID per category
    $random_ids_per_category[$category->term_id] = $random_post_id;
    }

    // Now, $random_ids_per_category contains a random ID per category

    $related_portfolio = new WP_Query(
    array(
    'post_type' => 'portfolio',
    'posts_per_page' => 4,
    'post__in' => $random_ids_per_category,
    'orderby' => 'post__in',
    )
    );

    Thanks in advance

    #1443018

    Hey Manel,
    Thank you for your patience, the taxonomy name would be a category, portfolio items are a post type.
    If you need further assistance please log in to the support forum and open a new thread If you are unable to login to the support forum because you don’t have a activate support contract, please try going to your Theme Forest account and renew your support and then log in to the support forum and open a new thread.
    Please note that using the contact form is not appropriate for support questions.

    Best regards,
    Mike

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.