Unable to query using WP_Query with new database instance - php

I'm writing REST api in wordpress. The api is in OOP format. I created new instance of $wpdb and the connection works just fine. the only problem I'm unable to query database via WP_Query(). But using normal sql, I can get the result.
Whenever I query using WP_Query, it's seems like returning me, the table columns with null value.
FAILS
$args = array(
'post_type' => 'custom_type'
);
$result = new WP_Query($args);
SUCCESS
$query = "SELECT * FROM table_name WHERE post_type ='custom_type'";
$result = get_results($query);
I also tried adding slash before WP_Query like this to no avail:
$result = new \WP_Query($args);
Where's my mistake here? Someone please advise me. Thanks in advance.

You should call have_posts() function in wpquery object
$query2 = new WP_Query( $args2 );
if ( $query2->have_posts() ) {
// The 2nd Loop
while ( $query2->have_posts() ) {
$query2->the_post();
echo '<li>' . get_the_title( $query2->post->ID ) . '</li>';
}
// Restore original Post Data
wp_reset_postdata();
}

I kindly suggest you to use query_posts instead of WP_Query and reset postdata after query.
query_posts lets you to use pagination, excerption and other features.
After query_posts you're free to use a simple wploop

Related

Wordpress WP_Query for multiple IDs returns null

I have following array:
array (12,15,21,32,33);
And then I use the following query to get the posts (of the above of IDs):
$the_query = new WP_Query( array("post__in"=>$ids_array) ); // edited
while($the_query->have_posts())
{
$the_query->the_post(); // added on edit, but still does not work
the_title()."<br />";
}
But I get nothing, no error and no interruption. I have checked the IDs and they are correct.
EDIT: I have put this query at the end of a module which is loaded into the footer. I don't know if it is important or not:
You forgot to add while ( $the_query->have_posts() ) : $the_query->the_post();.
You are just checking to see if you have posts, but doing nothing further
$ids_array = array (12,15,21,32,33);
$the_query = new WP_Query( array('post__in'=>$ids_array) );
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2></br>
<?php
endwhile;
wp_reset_postdata();
endif;
?>
EDIT
I think the underlying problem here is not this custom query, as this custom query works. It seems from your comments that you are using another custom query on the same page.
I don't know what the code of the first query looks like, but here are some troubleshooting points you need to go and have a look at
Most probably you did not reset the postdata on your first query. This will break your second query. wp_reset_postdata is extremely important when you are running custom queries with WP_Query and with get_posts. Check that you have used wp_reset_postdata after your first instance of WP_Query. Your first query should be in the same format as the one in my answer
You should use different variables for each instance of WP_Query. For example $variable1 = new WP_Query( YOUR ARGUMENTS ) for first instance and $variable2 = new WP_Query( YOUR ARGUMENTS ) for your second instance

Calling a specific row in single.php wordpress

I am trying to call a single row in each wordpress post. I was thinking that the best way to go around this is to insert a piece of code in the single.php file where I call via the post id:
<?php
$result = "SELECT post_date FROM wp_posts WHERE ID = get_the_ID()";
$row = mysql_fetch_assoc($result);
echo $row['post_date'];
?>
However this doesn't seem to work. Can anyone shed some light on why this isn't working?
Thanks!
WordPress offers a nice database abstraction layer to remove your need to read the posts table directly. It's apparent from your use of get_the_ID() that you're working with the "current" post or page in your code. So, guess what? That row of the posts table has already been loaded by WordPress. Try something like this to retrieve it.
$post = get_post();
echo $post->post_date;
See http://codex.wordpress.org/Function_Reference/get_post for more information.
The get_the_ID() in wordpress is a PHP function, not a SQL function. So try:
$result = "SELECT post_date FROM wp_posts WHERE ID = ".intval(get_the_ID());
*intval force the use of a number, and avoid the SQL injection.
<?php
global $post;
// $ids is an array with the required id's
$ids = array(1,2,24,234,2342);
$args = array(
'include' => $ids,
);
$myposts = get_posts($args);
//You always need $post, because setup_postdata assings eveyrhing to post...
//thats a Wordpres...way of things...
foreach($myposts as $post): setup_postdata($post):
the_time('F jS, Y');
//Don't forget to reset postdata
wp_reset_postdata();
endofreach;

Get Page by TAG ID in Wordpress

so i have a php script that need to retrieve a certain "page", not a post but a page, in wordpress, i want to retreive ONE page that is pointed in a specific tag. I would like to know how i can retreive that certain page, the only parameter it needs is the tag->term_id.
I tried searching in google to acheive this. is this possible?
Thanks..
use WP_Query with post_type set to page to query your database for pages. You can use the tag_id parameter to narrow down by tag. Limit it to one result using posts_per_page set to 1.
//narrow down your query with $args
$args = array('post_type'=>'page', 'tag_id'=>3, 'posts_per_page'=>1);
// The Query
$the_query = new WP_Query( $args );
// The Loop
while ( $the_query->have_posts() ) :
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
endwhile;
http://codex.wordpress.org/Class_Reference/WP_Query

Preserving postdata with embedded wp_query queries?

I'm designing a website in Wordpress. This site is one of those parallax sites where all the pages are printed on the homepage and the menu scrolls to the anchors.
That being said I am using a wp_query to pull out all the pages that are in the main menu. Furthermore I have a shortcode that I use in the content that also requires the use of wp_query.
The problem I have is that the shortcode (the embedded wp_query) is screwing up the postdata. I know when using wp_query you'd usually want to use wp_reset_postdata but in this particular situation it doesn't work because this function call will restore the postdata of the homepage and not of the currently running wp_query (sorry if I'm being unclear).
Is there a way to take a snapshot of the postdata to then restore after my shortcode? I'm looking for something along the lines of:
function my_shortcode() {
save_postdata(); //saves the current postdata
$query = new WP_Query();
while( $query->have_posts() ) {
$query->the_post();
echo get_the_title();
}
my_wp_reset_postdata(); //restores the postdata to where it was before the loop
}
By looking in the source for wp_reset_query(), you will see that what it does is that it simply restores the $wp_query global variable from another global variable($wp_the_query - this is set-up together with the initial set-up for $wp_query, so it holds the original query).
What you can do is you can simply assign $wp_query to a different global variable and then later restore it. Here's an example:
function _save_query( $var = '_wp_query' ) {
$GLOBALS[ $var ] = $GLOBALS['wp_query'];
}
function _wp_reset_query( $var = '_wp_query' ) {
$GLOBALS['wp_query'] = $GLOBALS[ $var ];
wp_reset_postdata();
}
So simply call _save_query() before overwriting the query(you can pass a custom variable name - this way you can store multiple WP_Query objects :) ).
Once you want to restore the query data, call _wp_reset_query() - again you can pass a string as a variable name in order to restore this exact query object.
This is how I managed to get it working, credit goes to Nikola's question since I worked off of his idea.
function _save_query( $var = '_wp_query' ) {
global $post;
$GLOBALS[ $var ] = $post;
}
function _wp_reset_query( $var = '_wp_query' ) {
global $post;
$post = $GLOBALS[ $var ];
setup_postdata( $post );
}
I looked at the documentation of how the loop works found here. I decided to use the same kind of setup as in Nikola's answer since it met my criteria but I used the implementation of the_post to restore the postdata. This is probably not very efficient since it's using the setup_postdata function (which I assume is overkill) but it has definitely solved my problem.
So now when I embed a wp_query I can just do the following:
_save_query();
$products = new WP_Query( $args );
if( $products->have_posts() ) {
$ob .= '<ul class="group-posts">';
while ( $products->have_posts() ) {
$products->the_post();
$ob .= '<li>'.get_the_title().'</li>';
}
_wp_reset_query();
$ob .= '</ul>';
}
Side question/note: What's the etiquette for marking an answer as the correct answer? I'd feel bad accepting my answer as the correct one when Nikola helped me reach it?
my_wp_reset_postdatadoes not exist. You have to use wp_reset_postdata(). But in a situation where you have to chain multiple wp_queries and come back to the older ones, you can store your first query in a variable, set the new WP_query, then reset it and come back to the old one.
$wp_query stores the current loop. So you can go something like :
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query($args);
...
// then later
$wp_query = $temp;
// And back on tracks !

How to fix this WordPress function so that it doesn't return a 404 page?

I have the following function that I've added to my functions.php file in WordPress. The idea is that it gathers all of the titles of 'fsmodel' posts (a custom post type that I've created). It then returns these as an array, which I then use to populate a select tag in the custom meta fields for a second custom post type.
Basically, 'fsmodel' will have posts with a boat model, and the 'fsboat' post type will have a drop-down with the names of each of the models to select from.
Now, this appears to works fine in the Dashboard - the drop-down is populated as expected. When I save, however, the post doesn't show up in the Edit list. Also on the website, all pages output as the 404 error page when this function is active.
I'm certain that the problem lies within the following code - does anyone have any idea what I might have done wrong?
function fs_model_array() {
$models_array = array();
$loop = new WP_Query(array(
'post_type' => 'fsmodel',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
'post_status' => 'publish'
));
while ( $loop->have_posts() ) : $loop->the_post();
$models_array[] = get_the_title();
endwhile;
return $models_array;
};
OK, I've come up with a solution (I hope - it's holding up for now).
Instead of creating a loop, I've just used the $wpdb->get_results to search the database for the column with a WHERE filter for the custom post type.
Then run an array builder:
$models_array = array();
$model_db = $wpdb->get_results("SELECT post_title FROM $wpdb->posts WHERE post_type='fsmodel' AND post_status = 'publish'");
foreach ($model_db as $model_db) {
$models_array[] = $model_db->post_title;
}
Thanks again for your time, hsatterwhite! :-)
I think you might find that adding wp_reset_query() to the end of your function will solve your problems :)
I like your solution, but I'd be inclined to say that you need to call the global variable of $post whenever you use the loop like this in a function, as it assigns it to that variable.
function fs_model_array(){
global $post;
$models_array = array();
$loop = new WP_Query(array(
...

Categories