WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm testing for a certain fixed post on an about page, I guessed that the best way is to test for post title (?).

Here's my code, what's going wrong?

if (is_page()) {
  $posts = get_posts ("cat=$cat&showposts=35");

  if ($posts) {
    foreach ($posts as $post):
      setup_postdata($post); ?>
        <div class="clear"></div>
            <h3><?php the_title(); ?></h3>
        <div class="clear"></div>

<?php    if ($post->post_title('skillset')) {
            echo '<div class="content">';
            echo '</div>'; 
share|improve this question
up vote 2 down vote accepted

First off all, $post is one of the WordPress core global variables and as such should either not be touched or reset after your foreach loop.

Using setup_postdata() allows you to make use of template tags such as you are doing in the above snippet with the_title(). It, or the loop it is used in, should however always be followed by its companion wp_reset_postdata().

Also $post is an object. Objects are instances of classes that contain properties and/or methods, the OOP equivalents of variables and functions.

By attempting to test for the post title in the way that you did,


you treated post_title as if it was a method and could be fed the title in question as an argument as well as would return a boolean. It however is a property and as such contains a value that you can access - and that is it.

This value then needs to be tested against another value. Hence

if( 'skillset' === $post->post_title ) {
    // your conditional operation

would be a working test for the title.

Essentially that line is the answer to the question in your title. Since you however asked what might be wrong with your code, also the showposts argument has been deprecated for a while. Use numberposts instead.

Also let's revisit the initial point on using $post. If instead of the template tags you'd reference the properties directly, it would not be necessary to overwrite the global $post.

$wpse67413_posts = get_posts();

foreach( $wpse67413_posts as $wpse67413_post ) {
     echo '<h3>' . $wpse67413_post->post_title . '</h3>';

Would work just as well without touching the global. That just for the sake of completeness.

And as a side note, why would you use that many php opening and closing tags in such little space? Pretty much a rhetorical question: It just doesn't make sense.

At last, are you sure, you want to do this:


? This only makes sense if you want to fetch posts of a category named exactly the same as the title of the page the visitor is on. Assuming the answer is positive, here's the complete correction of your above snippet:

global $post;

if( is_page() ) {
    $wpse67413_posts = get_posts( array(
        cat => get_cat_ID($post->post_title),
        numberposts => 35

    if( $wpse67413_posts ) {
        foreach ( $wpse67413_posts as $post ) {

            echo '<div class="clear"></div>' .
                '<h3>' get_the_title() . '</h3>' .
                '<div class="clear"></div>';

            if( 'skillset' === $post->post_title ) {
                echo '<div class="content">' . get_the_content() . '</div>'; 
            } else {

share|improve this answer

The best way would be to search for ID, not title! So then your if would be:

if ($post->ID === 123)

where 123 is the ID of the post you want to "filter".

share|improve this answer
And just in case you want to test for post title you would do this: if( $post->post_title == 'my title' ) – Abdussamad Oct 7 '12 at 23:07

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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