最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

post meta - get specific values from WordPress meta_value

programmeradmin1浏览0评论

a:1:{s:9:"toys_item";a:1:{s:16:"toys_item_ranger";a:11:{i:0;s:31:"2 x Seadoo GTX 230/IBR Jet-Skis";i:1;s:30:"1 x HD Runway 10′ Waterslide";i:2;s:14:"4 x F5 Seabobs";i:3;s:49:"3 x Water-Skis for children, teenagers and adults";i:4;s:43:"1 x 2 Person Nereus Kayak w/paddles & seats";i:5;s:14:"1 x Wake Board";i:6;s:35:"2 x Jobe Parana bamboo Paddleboards";i:7;s:44:"Seadoo Spark 900HO AXE 2UP/IBR Trixx Jet-Ski";i:8;s:31:"2 x Seadoo GTX 230/IBR Jet-Skis";i:9;s:14:"4 x F5 Seabobs";i:10;s:43:"1 x 2 Person Nereus Kayak w/paddles & seats";}}}

I want to show specific text from the above results, something like this:

2 x Seadoo GTX 230/IBR Jet-Skis
1 x HD Runway 10′ Waterslide
4 x F5 Seabobs
3 x Water-Skis for children, teenagers, and adults 1 x 2 Person Nereus Kayak w/paddles & seats
1 x Wake Board
2 x Jobe Parana bamboo Paddleboards
Seadoo Spark 900HO AXE 2UP/IBR Trixx Jet-Ski
2 x Seadoo GTX 230/IBR Jet-Skis
4 x F5 Seabobs
1 x 2 Person Nereus Kayak w/paddles & seats

here is my code

<?php
global $wpdb;
$results = $wpdb->get_results( "SELECT * FROM wp_postmeta where meta_key = 'fat-mb-boat-toys' AND post_id = $post_id"); // Query to fetch data from database table and storing in $results
if(!empty($results))
    // Checking if $results have some values or not
{    
        echo "<table>";
        echo "<tr>";
        echo "<th> Post ID <th>";
        echo "<th> Meta Key <th>";
        echo "<th> Meta Value <th>";
        echo "</tr>";

    foreach($results as $row){
       echo "<tr>";
       echo "<td>". $row->post_id ."<td>";
       echo "<td>". $row->meta_key ."<td>";
       echo "<td>". $row->meta_value ."<td>";
       echo "</tr>";
    }
        echo "<table>";


}
?> ```

a:1:{s:9:"toys_item";a:1:{s:16:"toys_item_ranger";a:11:{i:0;s:31:"2 x Seadoo GTX 230/IBR Jet-Skis";i:1;s:30:"1 x HD Runway 10′ Waterslide";i:2;s:14:"4 x F5 Seabobs";i:3;s:49:"3 x Water-Skis for children, teenagers and adults";i:4;s:43:"1 x 2 Person Nereus Kayak w/paddles & seats";i:5;s:14:"1 x Wake Board";i:6;s:35:"2 x Jobe Parana bamboo Paddleboards";i:7;s:44:"Seadoo Spark 900HO AXE 2UP/IBR Trixx Jet-Ski";i:8;s:31:"2 x Seadoo GTX 230/IBR Jet-Skis";i:9;s:14:"4 x F5 Seabobs";i:10;s:43:"1 x 2 Person Nereus Kayak w/paddles & seats";}}}

I want to show specific text from the above results, something like this:

2 x Seadoo GTX 230/IBR Jet-Skis
1 x HD Runway 10′ Waterslide
4 x F5 Seabobs
3 x Water-Skis for children, teenagers, and adults 1 x 2 Person Nereus Kayak w/paddles & seats
1 x Wake Board
2 x Jobe Parana bamboo Paddleboards
Seadoo Spark 900HO AXE 2UP/IBR Trixx Jet-Ski
2 x Seadoo GTX 230/IBR Jet-Skis
4 x F5 Seabobs
1 x 2 Person Nereus Kayak w/paddles & seats

here is my code

<?php
global $wpdb;
$results = $wpdb->get_results( "SELECT * FROM wp_postmeta where meta_key = 'fat-mb-boat-toys' AND post_id = $post_id"); // Query to fetch data from database table and storing in $results
if(!empty($results))
    // Checking if $results have some values or not
{    
        echo "<table>";
        echo "<tr>";
        echo "<th> Post ID <th>";
        echo "<th> Meta Key <th>";
        echo "<th> Meta Value <th>";
        echo "</tr>";

    foreach($results as $row){
       echo "<tr>";
       echo "<td>". $row->post_id ."<td>";
       echo "<td>". $row->meta_key ."<td>";
       echo "<td>". $row->meta_value ."<td>";
       echo "</tr>";
    }
        echo "<table>";


}
?> ```
Share Improve this question edited Mar 1, 2022 at 20:23 Pat J 12.4k2 gold badges28 silver badges36 bronze badges asked Feb 18, 2022 at 9:03 Pardeep KumarPardeep Kumar 11 bronze badge 2
  • You would need to first unserialize that data, then you can check the value using print_r to see what you type you are working with and what is available. – Buttered_Toast Commented Feb 18, 2022 at 9:08
  • can you please provide some reference? – Pardeep Kumar Commented Feb 18, 2022 at 9:30
Add a comment  | 

1 Answer 1

Reset to default 0

You should pretty much never need to use $wpdb to grab data. There are helper functions built into WordPress that will do what you need.

In this case, use get_post_meta().

Having used https://www.unserialize.com/ to unserialize your data, it looks like the item list in your question is in $meta['toys_item']['toys_item_ranger']. I'll use that in my code snippet to make a list like the one you're looking for.

$meta = get_post_meta( $post_id, 'fat-mb-boat-toys', true );
if (
    ! empty( $meta )
    && ! empty( $meta['toys_item'] )
    && ! empty( $meta['toys_item']['toys_item_ranger'] ) 
) {
    foreach ( $meta['toys_item']['toys_item_ranger'] as $toy ) {
        echo $toy . '<br />';
    }
}
发布评论

评论列表(0)

  1. 暂无评论