Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8088017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:58:07+00:00 2026-06-05T18:58:07+00:00

I am trying to make a toggle function work, but since I have only

  • 0

I am trying to make a toggle function work, but since I have only basic PHP knowledge, I just don’t know how to get my <div>‘s straight… Can somebody please help me?

My problem is not the (jQuery) toggle, but simply a combination of HTML/PHP used to get WordPress Custom Post Type specific content from the database. That worked fine with the code I have, but now I simply don’t know how to wrap the output into divs needed for my jQuery function.

So first:

Print out all meta_values by post_type. In this case the post_type = artists and the meta_value = artist_country.

I printed all artists belonging to a specific country like so:

Argentina (artists_country)
 - Capitan Tifus (artist title)
 - Kapanga (artist title)

Here comes my problem with the divs. I need to group (using ‘main1’) the artist titles that belong to a country, but without grouping the country itself.

<div class="group">
<h3><a class="trigger">Country 1</a></h3>
<div id='main' class='side-toggle'>
   <h4 class="date"><a href="<?php the_permalink(); ?> rel="fancybox" title="the title">The Artist 1 of that country</a></h4>
   <h4 class="date"><a href="<?php the_permalink(); ?> rel="fancybox" title="the title">The Artist 2 of that country</a></h4>
</div><!--main--></div><!--group-->

<div class="group">
<h3><a class="trigger">Country 2</a></h3>
<div id='main1' class='side-toggle'>
   <h4 class="date"><a href="<?php the_permalink(); ?> rel="fancybox" title="the title">The Artist 1 of country 2</a></h4>
   <h4 class="date"><a href="<?php the_permalink(); ?> rel="fancybox" title="the title">The Artist 2 of country 2</a></h4>
</div><!--main--></div><!--group-->

I am trying to figure out how to group the artists. But where ever I put my divs, I get 1 artist per country, our everything get’s looped multiple times. So I am guessing it’s a problem related to how I get the content from the database?

This is my code:

    // List posts by a Custom Field's values
    $meta_key = 'artists_country';  // The meta_key of the Custom Field
    $sql = "
   SELECT p.*,m.meta_value
   FROM $wpdb->posts p
   LEFT JOIN $wpdb->postmeta m ON (p.ID = m.post_id)
   WHERE p.post_type = 'artists'
      AND p.post_status = 'publish'
      AND m.meta_key = '$meta_key'
   ORDER BY 
    m.meta_value ASC, 
    p.post_title ASC
    ";
    $rows = $wpdb->get_results($sql);
    if ($rows) {

   foreach ($rows as $post) {
      setup_postdata($post);
      if ($post->meta_value != $current_value) {
          $current_value = $post->meta_value;
            echo ('<div class="group">'); 
                echo "<h3><a class='trigger' title='Click to expand'  rel='nofollow' href='#'>$post->meta_value  +</a></h2>";
                echo ("<div id='main1' class='side-toggle'>");
        }

      // Put code here to display the post
      echo ('<h4 class="date">');
      echo ('<a href="');
      the_permalink();
      echo ('" class="postlink" rel="fancybox" title="the title">');
      the_title();
      echo ('</a>');
      echo('</h4>');
     }
   echo('</div><!--main--> ');echo('</div><!--group--> '); 

    } 

Etc…

I have a link here: http://www.musicamestiza.nl/?page_id=4311

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T18:58:09+00:00Added an answer on June 5, 2026 at 6:58 pm

    After puzzling long time with the div’s and also trying many different ways to print posts by custom field in the above described order, moving the ending div to the start of the output did the trick some how combined with adding other divs (who don’t have any other function). (So I am starting the output with instead of .
    Not a great way to go, but it served my purpose and it might someone else’s. If anybody has a better solution I of course would like to know.

    <?php /* Template Name: Count */ ?>
    <div class="test">
    <?php // List posts by a Custom Field's values
    $meta_key = 'artists_country';  // The meta_key of the Custom Field
    $sql = "
    SELECT p.*,m.meta_value
    FROM $wpdb->posts p
    LEFT JOIN $wpdb->postmeta m ON (p.ID = m.post_id)
    WHERE p.post_type = 'artists'
      AND p.post_status = 'publish'
      AND m.meta_key = '$meta_key'
    ORDER BY 
    m.meta_value ASC, 
    p.post_title ASC 
    ";
    $rows = $wpdb->get_results($sql);
    
    if ($rows) {
    echo ('<div class="test">'); 
    
    foreach ($rows as $post) {
    
        setup_postdata($post);
        if ($post->meta_value != $current_value) {
                $current_value = $post->meta_value;
                echo "</div></div><div class='group2'><h3><a class='trigger2' title='Click to expand' rel='nofollow' href='#'>$post->meta_value  +</a></h3><div id='main1' class='side-left-toggle'>";
            }
    
    
    
        echo ('<h4 class="date">');
                echo ('<a href="');
                the_permalink();
                echo ('" class="postlink" rel="fancybox" title="the title">');
                the_title();
                echo ('</a>');
                echo('</h4>'); 
        }
        echo ('</div>');
    
     } //end if rows
    
    ?>
    </div>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to make a custom jQuery toggle function. Right now, I have 2
Im trying to make an accordion with toggle which seems to work pretty good
Trying to make simple minesweeper game in python, but have one problem. I have
I'm trying to make a regular button that functions like a toggle button, but
Im trying to toggle between two images but need the js to work with
I'm trying to make work a functionality that exists on my website but works
Trying to make a toggle button that hides/shows the right window/pane. Here is a
trying to make a page which will recursively call a function until a limit
I've been trying to make a button toggle a class, as well as show
I am trying to create a drop down list i have it working but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.