Results 1 to 8 of 8

Thread: php code widget used to pull post from loop

  1. #1
    DannyGSmith's Avatar
    DannyGSmith is offline Here For The Peanuts
    Join Date
    Jun 2009
    Location
    OKC OK
    Posts
    192

    Default php code widget used to pull post from loop

    I need to use a loop inside of a php code widget that will get a single post, with a given scientists (custom taxonomy) and a give category. I have gotten that to work fine, but the page will have more than one loop on it so I need to either reset the loop, clone it, or create a new query. I chose to try the later. For some reason, this code is not working. Can anyone see the problem?

    Code:
    <?php
       // we're looking at a page
       // get the current page's ID using the global $wp_query variable
       $ID = $wp_query->post->ID;
       
       // then get the permalink
       // so you can parse and get the last value
       // which is used as the value for the scientists taxonomy
       $permalink = get_permalink($ID);
       $stack = explode("/", $permalink);
    
       // permalink ends in / so you need to pop two
       $perm = array_pop($stack);
       $perm = array_pop($stack);   
       
       $contactPost = new WP_Query();
          
       // Get the only post that matches
       $contactPost->query_posts( array( 'scientists' => $perm, 'category_name' => 'contact-info', 'showposts' => 1 ) );
       while ($contactPost->have_posts()) : $contactPost->the_post(); 
          $contactPost->the_content();    
       endwhile;
    ?>
    The problem is with the WP_Query, I don't know if I need to include something else or what. I am stumped.

  2. #2
    wpmuguru is offline Here For The Peanuts
    Join Date
    Sep 2009
    Posts
    133

    Default

    Try changing

    'scientists' => $perm

    to

    'term' => $perm, 'taxonomy' => 'scientists' .
    Last edited by wpmuguru; 03-14-2010 at 07:51 PM. Reason: was missing the plural on scientist

  3. #3
    DannyGSmith's Avatar
    DannyGSmith is offline Here For The Peanuts
    Join Date
    Jun 2009
    Location
    OKC OK
    Posts
    192

    Default

    It seems strange, but that part works ok. It is when I have more than one loop on the page that I get into trouble, then only the first loop works ok. That is why I decided to create a new WP_Query. This is the error message I get:

    PHP Code:
    Error Code:                 php_code_error:1:/home/www/mockup/htdocs/wp-content/plugins/php-code-widget/execphp.php(43)  : eval()d code:20:Call to undefined method WP_Query::query_posts()                          Message:                 A fatal code error occurred

  4. #4
    Rarst's Avatar
    Rarst is offline Big Tipper
    Join Date
    Jul 2009
    Posts
    322

    Default

    I'd suggest get_posts() instead, but I am not too sure how it works with custom taxonomies. Google is bit confusing on the subject and I don't use taxonomies so can't test.

    Error is correct, because WP_Query doesn't have query_posts() method.

    query_posts() is separate function from WordPress query API http://codex.wordpress.org/Template_Tags/query_posts

    Upd.

    get_posts() can also take the parameters that query_posts() can since both functions now use the same database query code internally
    Seems it should be safe to use with taxonomies. get_posts() creates WP_Query as local variable so it seems better choice for multiply loops.
    Last edited by Rarst; 03-15-2010 at 03:00 AM.
    Rarst.net - cynical thoughts on software and web (and sometimes WP) | @Rarst | I seem to be non-GPL-compliant person. Beware my poisonous thoughts.

  5. #5
    Otto's Avatar
    Otto is offline On The Rocks
    Join Date
    Apr 2009
    Location
    Memphis, TN
    Posts
    862

    Default

    This is wrong:

    PHP Code:
    $contactPost = new WP_Query();
          
    // Get the only post that matches
       
    $contactPost->query_posts( array( 'scientists' => $perm'category_name' => 'contact-info''showposts' => ) ); 
    You can't do that. Try it like this instead:

    PHP Code:
    $contactPost = new WP_Query(array( 'scientists' => $perm,  'category_name' => 'contact-info''showposts' => )); 
    Also, this is wrong too:
    PHP Code:
    $contactPost->the_content(); 
    It should just be "the_content();". It's not a member function of that class.

    Finally, at the end of all that, you still need to make a call to wp_reset_query();, to avoid stomping on the main page's pre-existing Loop.

  6. #6
    Rarst's Avatar
    Rarst is offline Big Tipper
    Join Date
    Jul 2009
    Posts
    322

    Default

    Ok, more practical form of my morning ramblings.

    PHP Code:
    $ID $wp_query->post->ID;
    $permalink get_permalink($ID);
    $stack explode("/"$permalink);
    // two pops cut too much for me, no trailing slash in permalink
    $perm array_pop($stack);

    //  btw whouldn't this give same thing? not sure what your permalink structure is
    //    $perm=$wp_query->query_arguments->name;
    //    or for page
    //    $perm=$wp_query->query_arguments->pagename;

    $contactPost get_posts( array('scientists' => $perm,'category_name' => 'contact-info''showposts' => 1) );
    echo 
    $contactPost[0]->post_content
    I've tested it except taxonomy part. I should look into those some day...
    Rarst.net - cynical thoughts on software and web (and sometimes WP) | @Rarst | I seem to be non-GPL-compliant person. Beware my poisonous thoughts.

  7. #7
    DannyGSmith's Avatar
    DannyGSmith is offline Here For The Peanuts
    Join Date
    Jun 2009
    Location
    OKC OK
    Posts
    192

    Default

    That works perfectly! Thank you very much. I hope someday the taxonomies work as easy for hierarchical as they do for non. It can really help for complex sites.

    Thank you very much!!!

  8. #8
    Rarst's Avatar
    Rarst is offline Big Tipper
    Join Date
    Jul 2009
    Posts
    322

    Default

    You are welcome. :) Your logic was just fine, but choice of functions was wrong. Probably most common situation with coding for WP.
    Rarst.net - cynical thoughts on software and web (and sometimes WP) | @Rarst | I seem to be non-GPL-compliant person. Beware my poisonous thoughts.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •