-1

How to i foreach the post title like below. this is what i want below

function get_all_post_for_search() {
    return array (
      array(
        'name' => "Hello Project",
        'thumb' => '/wp-content/uploads/2016/03/photo-1442473483905-95eb436675f1.jpeg',
        'desc' => 'Lorem ipsum dolor sit amet, consectetur adipisicing elit..'
      ),
      array(
        'name' => "Another Project",
        'thumb' => '/wp-content/uploads/2016/03/photo-1449024540548-94f5d5a59230-1.jpeg',
        'desc' => 'Lorem ipsum dolor sit amet, consectetur adipisicing elit..'
      )
    );
}

this is my code

function all_post_data(){

    $args = array (
        'post_type'              => 'post',
        'pagination'             => FALSE,
        'suppress_filters'       => FALSE
    );

    $query = new WP_Query( $args );

    if($query->have_posts()):
        while($query->have_posts()):
            $query->the_post();

            $post_id = get_the_id();
            $the_title = get_the_title();
            $thumbs = get_post_thumbnail_id();
            $des = get_the_content();
            $search_data = [];

                  $img_url = $thumb['url'];

                  if(is_array($thumb) && $img_url){
                      $search_data[] = array(
                          'name' => $the_title,
                          'thumb' => $img_url,
                          'desc' => $des
                      );
                  }       
        endwhile;
    endif;

    wp_reset_postdata();

    var_dump($search_data);
}
  • What is the current output ? – Sumit Apr 21 '16 at 7:45
  • just empty array:( – Mohamed Rihan Apr 21 '16 at 7:50
1

Your code has many errors:-

  • $thumbs isn't used anywhere.
  • $img_url = $thumb['url']; undefined variable
  • if(is_array($thumb) && $img_url) always false because of second point.
  • $search_data = []; should be outside of loop.

Check the correct part of code

$search_data = [];
if($query->have_posts()):
    while($query->have_posts()):
        $query->the_post();

        $img_url = get_the_post_thumbnail_url();

        if(!empty($img_url)){
            $search_data[] = array(
                'name' => get_the_title(),
                'thumb' => $img_url,
                'desc' => get_the_content()
            );
        }       
    endwhile;
endif;

NOTE: get_the_post_thumbnail_url() is only available from WordPress 4.4

  • I thinks this code is working – Mohamed Rihan Apr 21 '16 at 8:21
  • What do you mean by think ? It is really working or not working ? – Sumit Apr 21 '16 at 8:40
-1

working code

function list_post_data(){
   $args="SELECT * FROM wp_posts WHERE wp_posts.`post_type` = 'post' AND wp_posts.`post_status` = 'publish'";

    $args = array(
          'post_type' => 'post'
            );

    $loop = new WP_Query( $args );

    $array = array();

    while ( $loop->have_posts() ) : $loop->the_post();

        global $post;
        $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
        $array[] = array(
            //'id' => get_the_ID(),
            'name' => get_the_title(),
            'thumb' => $url,
            'desc' => get_the_content()
        );

    endwhile;

   // wp_reset_query();
    ob_clean();
    //echo json_encode($array);
    var_dump($array);
    exit();
}

result is

array (size=2)
  0 => 
    array (size=3)
      'name' => string 'test post' (length=9)
      'thumb' => boolean false
      'desc' => string 'dfgrgsrga' (length=9)
  1 => 
    array (size=3)
      'name' => string 'Hello world!' (length=12)
      'thumb' => string 'http://lfigp-new.dev/wp-content/uploads/2016/03/valentine-cake_1x.png' (length=69)
      'desc' => string 'Welcome to WordPress. This is your first post. Edit or delete it, then start writing!' (length=85)
  • Also, explain what you have done and how the code works. Code dumping are poor quality answers. Please see How to Answer – Pieter Goosen Apr 22 '16 at 8:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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