1

I'm missing something here.
I simply want to get data from my database. So I tried this but keeps giving me an error.

global $wpdb;
$sql = "SELECT * FROM $wpdb->postmeta WHERE group='$group'";
$results = $wpdb->get_results($sql) or die(mysql_error());

foreach( $results as $result ) {

    echo $result->company_name;

}

$group is a parameter passed in the url the variable is $group = $_GET['group'];

This is the error:

WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'group='bicyclegroup'' at line 1] SELECT * FROM xx_postmeta WHERE group='thegroup' You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'group='thegroup'' at line 1

Can somebody please help me fix this once and for all so I can save it locally and use it for further projects :-)

1
  • the or die( mysql_error() ); is bad, in newer versions of WordPress and PHP the mysql extension isn't even used ( the mysql extension and all the mysql_ functions are deprecated in favour of mysqli, you should avoid them ). Instead check the value of $results and check for an error value
    – Tom J Nowell
    Commented Mar 18, 2015 at 14:51

1 Answer 1

4

It may be because group doesn't exist in the postmeta table. Your meta_key is probably called group and has a meta_value of bicyclegroup. This is assuming you haven't modified the postmeta table. What you could try is:

$sql = "SELECT * FROM $wpdb->postmeta WHERE meta_key = 'group' AND meta_value='$group'";

This will return:

  • meta_id
  • post_id
  • meta_key ( group )
  • meta_value ( bicyclegroup )

Another option is to get all posts by post meta is a secondary query WP_Query. To do so would look like this:

$test_posts = new WP_Query( array(
    'post_type'      => 'post',
    'posts_per_page' => -1,
    'meta_key'       => 'group',
    'meta_value'     => $group
) );

<?php if( $test_posts->have_posts() ) : ?>

    <?php while( $test_posts->have_posts() ) : $test_posts->the_post(); ?>

        <h1><?php the_title(); ?></h1>
        <?php the_content(); ?>

    <?php endwhile; ?>

<?php endif; ?>

I'm not sure if you're using post are your post type so change that field accordingly.

5
  • I think I made a mistake in my thinking. I have the wp_postmeta table filled with different page attributes. I want to extract all other page attributes where the group is $group. So basically filter all pages that have the group $group. Commented Mar 18, 2015 at 14:11
  • @Interactive What you're looking for sounds like a WP_Query, one second let me write it up. Another option is you could use the $post_id's that get returned from that SQL to then get_post(). Finally, another option is to modify the main query by using pre_get_posts. You may be better off creating a new question and explaining your problem then show what you've tried to solve it ( you code above ).
    – Howdy_McGee
    Commented Mar 18, 2015 at 14:25
  • 1
    This answer to »getting all values for a custom field key (cross-post)« offers an ready-made function for the $wpdb call, which can be customized for your use. @Interactive On a second thought, its not exactly what you want, but maybe helpful anyway. Commented Mar 18, 2015 at 14:32
  • Thank you so much for your answers and time. In the meantime I used get_post like you suggested. What would be wiser? Use the get_post or use your WP_Query? Commented Mar 18, 2015 at 14:32
  • 1
    @Interactive probably WP_Query - it's more powerful and it makes less DB calls, sounds what you're looking for - getting the actual posts.
    – Howdy_McGee
    Commented Mar 18, 2015 at 14:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.