• Resolved arhamm

    (@arhamm)


    I have a custom post type called “Console”. One of the fields is called “model”. “Model” is a custom defined list where these are some of my options: Xbox One X, Xbox One S, PS4 …etc.

    I want to display all of these “models” in a table. This is what I got:

    
    function buying_report() {
    
        $models = array(
            'post_type' => 'console',
            'post_status' => 'publish',
        );
    
        $query = new WP_Query($models);
    
        if ($query->have_posts()) {
    
            echo "<table border=\"1\" align=\"center\">";
            echo "<tr><th>Model</th>";
            echo "<th>Price</th></tr>";
    
            while ($query->have_posts()) {
                $query->the_post();
                
                echo "<tr><td>";
                echo get_field( get_the_id(), 'model', true);
                echo "</td><td>";
                echo "</td></tr>";
    
            }
        }
       
        echo "</table>";
    
    } 

    This works but is giving me a lot of duplicate models. Am I doing it correctly?

Viewing 1 replies (of 1 total)
  • Plugin Author Jory Hogeveen

    (@keraweb)

    Hello @arhamm

    You are currently querying for ALL consoles and then for ALL their models. This is why you are getting duplicated results.

    If you only want to show the models of the current console you’ll have to get the current console object, not a list of consoles through WP_Query.

    Cheers, Jory

Viewing 1 replies (of 1 total)
  • The topic ‘echo custom field in a table’ is closed to new replies.