$wpdb->get_results issue

I have a $wpdb->get_results query that looks like this:

"SELECT *
 FROM $wpdb->posts
 WHERE post_author = $current_user_id
   AND post_status = 'publish'
   AND post_type = 'event'
 ORDER BY post_date DESC"

It translates to:

Read More
SELECT *
FROM wp_posts
WHERE post_author = 5
  AND post_status = 'publish'
  AND post_type = 'event'
ORDER BY post_date DESC

If I run that exact query in the database via phpmyadmin, it returns results perfectly fine. In the wordpress implementation however it doesn’t return anything.
I’ve narrowed it down to the AND post_type = 'event' part that seems to be breaking it. When I leave that away from the query, it returns results just fine

Anyone know why?

Thanks!

Related posts

Leave a Reply

4 comments

  1. To my understanding, wordpress has 5 major post types: (post, page, attachment, revision, and nav_menu_item )

    In order to look up a post_type of ‘event’ you need to have registered this custom post type using the register_post_typefunction.

    Here is a sample of how you might create a custom post type called ‘event’

    add_action( 'init', 'create_post_type' );
    function create_post_type() {
        register_post_type( 'event',
            array(
                'labels' => array('name' => __( 'events' ),
                        'singular_name' => __( 'event' )),
                'public' => true,
            )
        );
    }
    

    Note: register_post_type($post_type, $args) takes 2 arguments by default.

    $post_type – is the name of your custom post type.

    $args – is any number of arguments to modify the default behavior of the post type. This should usually be an array.

    The 'labels' argument describes the name of the post type in general and singular form. If you prefer to have general and singular versions of the post type then use the method shown. Otherwise the default is just the $post_type name name that you supplied.

    'public' argument says if the post type is intended for public use.

    You can also write a simplified version without many tags such as:

    add_action( 'init', 'create_post_type' );
    function create_post_type() {
        register_post_type( 'event', array( 'public' => true, 'label' => 'events' ) );
    }
    

    Also note that create_post_type is a custom name for a function that you could call my_super_cool_new_custom_original_post_type_creating_function haha or something descriptive for your needs.

    In the simplified version I used label which takes a single argument and that is the general name of the post type. If you would like to add a description for your post types you would do it within the parentheses for the name and replace one underscore for an x such as:

    add_action( 'init', 'create_post_type' );
    register_post_type( 'event',
        array(
            'labels' => array('name' => _x( 'events' , 'post type general name' ),
                    'singular_name' => _x( 'event' , 'post type singular name' )),
            'public' => true,
        )
    );
    

    Word of caution: Do not use register_post_type before init.

    Now, if you have done all of this and you can’t access the post_type, ensure you have added the public argument to it. 🙂

    cheers

  2. Try using post_type like %event%

    You can execute the same thing via WP_Query query_posts, this is not a complicated query where you need to use $wpdb but I might be wrong.

  3. try the below code :

    $get_post = $wpdb->query("SELECT * FROM wp_posts
    WHERE post_author = ".$current_user_id."
      AND post_status = 'publish'
      AND post_type = 'event'
    ORDER BY post_date DESC
    "); 
    

    OR

    $wpdb->get_results("SELECT *
     FROM $wpdb->posts
     WHERE post_author = $current_user_id
       AND post_status = 'publish'
       AND post_type = 'event'
     ORDER BY post_date DESC", OBJECT);
    
  4. The problem I had was coming from the new lines. For some reason either the editor or the db was configured wrong, as it was never happened to me.

    Once I changed the query to be on the same lines it started to work.