Skip to main content
added 259 characters in body
Source Link
dingo_d
  • 1.9k
  • 1
  • 25
  • 45

I need to query all posts using SQL (tried doing it with WP_Query didn't work correctly), of certain post types

$post_types = get_post_types(); 
unset( $post_types['attachment'], $post_types['nav_menu_item'] );

$post_type_string = '\'' . implode( '\', \'', $post_types ) . '\'';

global $wpdb;
$replace_query = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE post_type IN ( %s )", $post_type_string ), ARRAY_A );

The $post_types array will give me

array( 'post' => 'post', 'page' => 'page', 'revision' => 'revision', 'custom_css' => 'custom_css', 'attachment' => 'attachment', 'nav_menu_item'=> 'nav_menu_item', 'customize_changeset' => 'customize_changeset', 'tt_font_control' => 'tt_font_control' )

I imploded this array, to add quotes around the values, and put this in my $wpdb->prepare as shown, but all I got was empty array.

When I removed the prepare and did this

$replace_query = $wpdb->get_results( "SELECT * FROM $wpdb->posts WHERE post_type IN ( $post_type_string )", ARRAY_A );

The query worked and I got back full array.

Why did this happen?

The $post_type_string is a string, so %s is correct in the prepare.

I tried not 'quoting' the values in the string, but that didn't work as well, and I tried with double quotes. I know that prepare does

$query = str_replace( "'%s'", '%s', $query ); // in case someone mistakenly already singlequoted it
$query = str_replace( '"%s"', '%s', $query ); // doublequote unquoting
$query = preg_replace( '|(?<!%)%f|' , '%F', $query ); // Force floats to be locale unaware
$query = preg_replace( '|(?<!%)%s|', "'%s'", $query ); // quote the strings, avoiding escaped strings like %%s
array_walk( $args, array( $this, 'escape_by_ref' ) );
return @vsprintf( $query, $args );

}

from developers prepare docs. Does this happen because of the quotes? And if so, how to prepare my query correctly?

EDIT:

The WP_Query that doesn't work as expected

$temp_posts = new WP_Query( array(
            'post_type' => $post_types,
            'posts_per_page' => 1000000,
            'no_found_rows' => true,
            'post_status' => array( 'any', 'draft' )
            )
        );

I need to query all posts using SQL (tried doing it with WP_Query didn't work correctly), of certain post types

$post_types = get_post_types(); 
unset( $post_types['attachment'], $post_types['nav_menu_item'] );

$post_type_string = '\'' . implode( '\', \'', $post_types ) . '\'';

global $wpdb;
$replace_query = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE post_type IN ( %s )", $post_type_string ), ARRAY_A );

The $post_types array will give me

array( 'post' => 'post', 'page' => 'page', 'revision' => 'revision', 'custom_css' => 'custom_css', 'attachment' => 'attachment', 'nav_menu_item'=> 'nav_menu_item', 'customize_changeset' => 'customize_changeset', 'tt_font_control' => 'tt_font_control' )

I imploded this array, to add quotes around the values, and put this in my $wpdb->prepare as shown, but all I got was empty array.

When I removed the prepare and did this

$replace_query = $wpdb->get_results( "SELECT * FROM $wpdb->posts WHERE post_type IN ( $post_type_string )", ARRAY_A );

The query worked and I got back full array.

Why did this happen?

The $post_type_string is a string, so %s is correct in the prepare.

I tried not 'quoting' the values in the string, but that didn't work as well, and I tried with double quotes. I know that prepare does

$query = str_replace( "'%s'", '%s', $query ); // in case someone mistakenly already singlequoted it
$query = str_replace( '"%s"', '%s', $query ); // doublequote unquoting
$query = preg_replace( '|(?<!%)%f|' , '%F', $query ); // Force floats to be locale unaware
$query = preg_replace( '|(?<!%)%s|', "'%s'", $query ); // quote the strings, avoiding escaped strings like %%s
array_walk( $args, array( $this, 'escape_by_ref' ) );
return @vsprintf( $query, $args );

}

from developers prepare docs. Does this happen because of the quotes? And if so, how to prepare my query correctly?

I need to query all posts using SQL (tried doing it with WP_Query didn't work correctly), of certain post types

$post_types = get_post_types(); 
unset( $post_types['attachment'], $post_types['nav_menu_item'] );

$post_type_string = '\'' . implode( '\', \'', $post_types ) . '\'';

global $wpdb;
$replace_query = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE post_type IN ( %s )", $post_type_string ), ARRAY_A );

The $post_types array will give me

array( 'post' => 'post', 'page' => 'page', 'revision' => 'revision', 'custom_css' => 'custom_css', 'attachment' => 'attachment', 'nav_menu_item'=> 'nav_menu_item', 'customize_changeset' => 'customize_changeset', 'tt_font_control' => 'tt_font_control' )

I imploded this array, to add quotes around the values, and put this in my $wpdb->prepare as shown, but all I got was empty array.

When I removed the prepare and did this

$replace_query = $wpdb->get_results( "SELECT * FROM $wpdb->posts WHERE post_type IN ( $post_type_string )", ARRAY_A );

The query worked and I got back full array.

Why did this happen?

The $post_type_string is a string, so %s is correct in the prepare.

I tried not 'quoting' the values in the string, but that didn't work as well, and I tried with double quotes. I know that prepare does

$query = str_replace( "'%s'", '%s', $query ); // in case someone mistakenly already singlequoted it
$query = str_replace( '"%s"', '%s', $query ); // doublequote unquoting
$query = preg_replace( '|(?<!%)%f|' , '%F', $query ); // Force floats to be locale unaware
$query = preg_replace( '|(?<!%)%s|', "'%s'", $query ); // quote the strings, avoiding escaped strings like %%s
array_walk( $args, array( $this, 'escape_by_ref' ) );
return @vsprintf( $query, $args );

}

from developers prepare docs. Does this happen because of the quotes? And if so, how to prepare my query correctly?

EDIT:

The WP_Query that doesn't work as expected

$temp_posts = new WP_Query( array(
            'post_type' => $post_types,
            'posts_per_page' => 1000000,
            'no_found_rows' => true,
            'post_status' => array( 'any', 'draft' )
            )
        );
Source Link
dingo_d
  • 1.9k
  • 1
  • 25
  • 45

$wpdb->prepare affecting the query?

I need to query all posts using SQL (tried doing it with WP_Query didn't work correctly), of certain post types

$post_types = get_post_types(); 
unset( $post_types['attachment'], $post_types['nav_menu_item'] );

$post_type_string = '\'' . implode( '\', \'', $post_types ) . '\'';

global $wpdb;
$replace_query = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE post_type IN ( %s )", $post_type_string ), ARRAY_A );

The $post_types array will give me

array( 'post' => 'post', 'page' => 'page', 'revision' => 'revision', 'custom_css' => 'custom_css', 'attachment' => 'attachment', 'nav_menu_item'=> 'nav_menu_item', 'customize_changeset' => 'customize_changeset', 'tt_font_control' => 'tt_font_control' )

I imploded this array, to add quotes around the values, and put this in my $wpdb->prepare as shown, but all I got was empty array.

When I removed the prepare and did this

$replace_query = $wpdb->get_results( "SELECT * FROM $wpdb->posts WHERE post_type IN ( $post_type_string )", ARRAY_A );

The query worked and I got back full array.

Why did this happen?

The $post_type_string is a string, so %s is correct in the prepare.

I tried not 'quoting' the values in the string, but that didn't work as well, and I tried with double quotes. I know that prepare does

$query = str_replace( "'%s'", '%s', $query ); // in case someone mistakenly already singlequoted it
$query = str_replace( '"%s"', '%s', $query ); // doublequote unquoting
$query = preg_replace( '|(?<!%)%f|' , '%F', $query ); // Force floats to be locale unaware
$query = preg_replace( '|(?<!%)%s|', "'%s'", $query ); // quote the strings, avoiding escaped strings like %%s
array_walk( $args, array( $this, 'escape_by_ref' ) );
return @vsprintf( $query, $args );

}

from developers prepare docs. Does this happen because of the quotes? And if so, how to prepare my query correctly?