Search code examples
phpwordpressposts

Get (all) post data of specific category into a variabele


I have some problems getting all the data of a post. I have tried several things but can't get it to work. What can I do?

Here is a function I wrote to get the post data:

function wptGetPostData( $sGategory, $sField = null, $iIndex = null )
{
 global $WPT_POST_DATA, $wp_query;

 if( !is_array( $WPT_POST_DATA ))
 {
  var_dump( $sGategory ); 
  $WPT_POST_DATA = array(); 
  $oOpt = array(
              'category_name' => $sGategory, // get posts by category name
             'posts_per_page' => -1 // all posts
             ); 

  query_posts( $oOpt );
  // var_dump( have_posts() );
   while(have_posts())
   { 
     var_dump( 'loop' );
     $WPT_POST_DATA[] = $wp_query->get_post_format(); //(array('echo'=>false));
     //echo the_title();
     //echo the_content(); 
   }
  //var_dump( $wp_query->posts ); die;
   wp_reset_query();
 } 
 var_dump( $WPT_POST_DATA );

 // other code here ...

 return null;
}

The $WPT_POST_DATA is a cache and must contain all posts (max 5 elements). Can somebody help me with this?


Solution

  • Got it!

    function suIsValidString( &$s, &$iLen = null, $minLen = null, $maxLen = null )
    {
      if( !is_string( $s ) || !isset( $s{0} ))
       { return false; }
    
      if( $iLen !== null )
       { $iLen = strlen( $s ); }
    
      return (( $minLen===null?true:($minLen > 0 && isset( $s{$minLen-1} ))) && 
               $maxLen===null?true:($maxLen >= $minLen && !isset( $s{$maxLen})));   
    }   
    
    
    // -- ** -- Template functions
    
    function wptGetCategoryIdByName( $sName )
    {
     $oTerm = get_term_by( 'name', $sName, 'category' );
     return (is_object( $oTerm ) && isset( $oTerm->term_id ))?$oTerm->term_id:null;
    }
    
    function wptGetPostData( $sCategory, $sField = null, $iIndex = null ) 
    {
    
     global $WPT_POST_DATA;
     $bCategory   = ( suIsValidString( $sCategory ) ); 
     $bIsInit     = ( is_array( $WPT_POST_DATA ));
     $uIndex      = $bCategory?$sCategory:0;
     $sFieldName  = ( suIsValidString( $sField )?$sField:'content' );
    
     if( !$bIsInit || !isset( $WPT_POST_DATA[$uIndex] ))
     {
       if( !$bIsInit )
        { $WPT_POST_DATA = array(); }
    
       // wp_reset_query();  
       //$category_query = new WP_Query( array( 'cat' => wptGetCategoryIdByName($sCategory)) );
      $bValid = true;
      $iCategory = null;     
      if( $bCategory )
       {
         $iCategory = wptGetCategoryIdByName($sCategory);
         $bValid = ( is_numeric( $iCategory ) && $iCategory >= 0 );
         if( !$bValid )
          { $iCategory = null; }
       }    
    
       $sQuery = 'numberposts=-1'.(($iCategory !== null)?('&cat='.$iCategory):null);
       $WPT_POST_DATA[$uIndex] = $bValid?get_posts( $sQuery ):array();
        /*
       if( $bCategory )
         { 
          var_dump( $uIndex ); 
          var_dump( $WPT_POST_DATA[$uIndex] );  } 
       */
       if( !is_array( $WPT_POST_DATA[$uIndex] ))
        {  $WPT_POST_DATA[$uIndex] = array(); } 
     } 
    
     if( $sFieldName === 'count' )
      { 
        //var_dump( $WPT_POST_DATA[$uIndex] ); die;
        return count( $WPT_POST_DATA[$uIndex] ); 
     } 
    
    $iAutoIndex = 0;
     if( !isset( $WPT_POST_DATA[$uIndex]['autocounter'] ))
      { $WPT_POST_DATA[$uIndex]['autocounter'] = $iAutoIndex; }
     else { $iAutoIndex=(++$WPT_POST_DATA[$uIndex]['autocounter']); }
     if( !is_numeric( $iIndex ))
      { $iIndex = $iAutoIndex; }
    
    
    
     $uResult = null;
     // var_dump( $WPT_POST_DATA[$uIndex][$iIndex]->post_content );
     if( isset( $WPT_POST_DATA[$uIndex][$iIndex])  && is_object( $WPT_POST_DATA[$uIndex][$iIndex] ))
     {
      $sPostPrefixName = 'post_'.$sFieldName;
      if( isset( $WPT_POST_DATA[$uIndex][$iIndex]->$sPostPrefixName ))
       { $uResult = &$WPT_POST_DATA[$uIndex][$iIndex]->$sPostPrefixName; }
      elseif( isset( $WPT_POST_DATA[$uIndex][$iIndex]->$sFieldName ))
       { $uResult = &$WPT_POST_DATA[$uIndex][$iIndex]->$sFieldName; }
     }
    
     if( suIsValidString( $uResult ))
     {
        $uResult = utf8_decode( $uResult ); 
        suSpecialCharsToHtml( $uResult );
     } 
     //var_dump( $uResult );
     return $uResult;
    }
    

    Usage:

      $sMyString = wptGetPostData( 'mycat', 'content' );
      var_dump( $sMyString );
    

    That's it!