Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want a query to be executed IF the following condition is true... but looks like the query is executed anyways.

if($post->post_parent = '302'){
// Query goes here
}

Is this query fine?? I want to executed the query if the parent page of current page is 302.

Thanks

share|improve this question
add comment

closed as too localized by toscho Jul 31 '12 at 5:55

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

2 Answers

up vote 0 down vote accepted

The following solution worked

$query2= mysql_query("SELECT * FROM wp_posts WHERE ID='$post->ID' AND post_parent=302");
$numrows2 = mysql_num_rows($query2);
if($numrows2 != 0) {
// query goes here
}

share|improve this answer
    
You shouldn't normally be writing SQL queries in WordPress. Instead, use the API. In this case, you should have used get_posts. When you do need to write queries, use WPDB instead of interacting directly with MySQL. –  Ian Dunn Jul 31 '12 at 6:26
    
is there any significant drawback of it ?? –  Nasir Zia Jul 31 '12 at 7:33
    
One of the biggest is that your queries could break after an upgrade if the database schema changes. Another big one is that you miss out on the internal caching that WordPress does, so your site will be slower. Another is that the API provides an easy way to escape queries with $wpdb->prepare(), which is a good security habit. –  Ian Dunn Jul 31 '12 at 14:48
add comment

Try adding global $post; to the function, so that the $post variable will be available within the function's scope. Also, make sure your code is executing inside The Loop. If the function is executing too early, then $post may not be available.

If neither of those things work, please post the full function and any other relevant code or details.

share|improve this answer
    
Sorted....! I used another query to find the current post's parent.. then compared it with 302.. –  Nasir Zia Jul 31 '12 at 4:47
add comment

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