0

How to get all posts from one category. i tried this code , it's not showing any output.Is it correct or any correction is here? Thanks.

  include('wp-config.php');


  global $wp_query;

 $args = ('category=news&posts_per_page=-1');

 $myposts = get_posts( $args );


 foreach ( $myposts as $post ) : setup_postdata( $post ); 
   $result =  array(
   "id"=>$args['ID'],
   "type"=>$args['post_type'],
   "url"=>$args['guid']);
 endforeach; 

wp_reset_postdata();
print($result);
FluffyKitten
  • 13,824
  • 10
  • 39
  • 52
Guna
  • 15
  • 7
  • Where are you running this code and why do you call `global $wp_query`. Why not make use of the [`WP_POST` Member Variables](http://codex.wordpress.org/Class_Reference/WP_Post). – Pieter Goosen Jul 25 '14 at 09:06
  • Am just create one php file and testing. i want to display post from one category, but in array. can you help me. – Guna Jul 25 '14 at 09:28
  • What do you mean "in array" – Pieter Goosen Jul 25 '14 at 09:29
  • `$result = array( "id"=>$args['ID'], "type"=>$args['post_type'], "url"=>$args['guid']);` Result should be inside array – Guna Jul 25 '14 at 09:51

4 Answers4

2

Try below :-

    global $wp_query;

 $args = ('category=news&posts_per_page=-1');

 $myposts = get_posts( $args );


 foreach ( $myposts as $post ) : setup_postdata( $post ); 

   $result[] =  array(
   "id"=>$post->ID, // changed $args to $post
   "type"=>$post->post_type,
   "url"=>$post->guid);
 endforeach; 

wp_reset_postdata();
print_r($result);
Khushboo
  • 1,819
  • 1
  • 11
  • 17
  • It's not showing anything. did u chk in your local? – Guna Jul 25 '14 at 10:00
  • please check now. I have made changes – Khushboo Jul 25 '14 at 10:06
  • One more help. did u chk my another question about mysql? if u knw the answer pls help me – Guna Jul 25 '14 at 10:19
  • where is the another question ? – Khushboo Jul 25 '14 at 10:20
  • http://stackoverflow.com/questions/24931531/get-posts-from-all-categories-except-one-category?noredirect=1#comment38777535_24931531 – Guna Jul 25 '14 at 10:23
  • Hi, the result shows only one post.other posts are not showing – Guna Jul 26 '14 at 06:49
  • `global $wp_query; $args = ('category=news&posts_per_page=-1'); $myposts = get_posts( $args ); foreach ( $myposts as $post ) : setup_postdata( $post ); $result[] = array( "id"=>$post->ID, // changed $args to $post "type"=>$post->post_type, "url"=>$post->guid, "status"=>$post->post_status, "post_count"=>COUNT(id)); endforeach; wp_reset_postdata(); print_r($result);` – Guna Jul 26 '14 at 08:47
  • i tried this code for find the post count. `"post_count"=>COUNT(id)` – Guna Jul 26 '14 at 08:50
  • it shows post count is 1. but total no of post is 10. – Guna Jul 26 '14 at 09:14
  • try count($result[0]), after endforeach – Khushboo Jul 26 '14 at 09:31
  • can u tell ur official mail id.i 'll send my code.i can't post here. – Guna Jul 28 '14 at 06:02
  • after foreach, then how i can use count inside loop? – Guna Jul 28 '14 at 06:41
  • why you want count inside foreach() ? – Khushboo Jul 28 '14 at 06:55
  • Am writing web services. so i have to show "post_count" inside array...if i try "count($result[0])" after foreach , then how can i use inside array. – Guna Jul 30 '14 at 04:43
  • How do i get these class details using jquery? `text =$(this).find('.image_name').text();` – Guna Jul 30 '14 at 05:01
  • `$("#imgClick").live('click',function(){ var x=[]; u=0; $("input:checkbox[name=imageselect]:checked").each(function() {u++; var lab=$(this).attr('id'); var m=$("#"+lab).html(); x.push(m); }); var sample=''; if(u==0){ alert("no item selected"); $("#printorder").hide(); $("#printorderArab").hide(); $("#customerPhotos").show(); } sample+='
    '+x[i]+'
    ` The process is, i can select images from products page and then purchase.in products page, images are showing, when i go to purchase page image is not showing. they used +x[i]+ code for images.
    – Guna Jul 30 '14 at 06:30
  • I think you should ask these questions in another new post. so that any other person can also reply :) – Khushboo Jul 30 '14 at 06:39
  • yeah i knw..but i need but ur help – Guna Jul 30 '14 at 06:43
  • shall i send the link of my website – Guna Jul 30 '14 at 06:48
  • select images & click "choose size & quantity" button,images are not showing."undefined" – Guna Jul 30 '14 at 06:52
  • I cannot see "choose size & quantity" button. Please send me the link for the product page – Khushboo Jul 30 '14 at 06:55
  • u have to login, then only u can see that page.how i send in public – Guna Jul 30 '14 at 06:59
  • Hi leave all above things. How to get commented post from all categories . I tried this code, `$sql_qry="SELECT * FROM news_posts, news_comments WHERE ID = '$Commentpostid' AND ID = comment_ID GROUP BY ID";`sometime it works, fewtimes not working. can u help me? – Guna Jul 30 '14 at 11:34
  • http://stackoverflow.com/questions/25052874/do-i-have-to-approve-post-comments-in-wordpress – Guna Jul 31 '14 at 08:40
  • chk this link & help me – Guna Jul 31 '14 at 08:40
1

are you just trying to get posts from a category?

This is a handy code from the Codex that I keep around. Use this on any custom category page or anywhere on any page, for that matter, to start the loop. Make sure you put your category slug into the right spot in the code.

query_posts( array ( 'category_name' => 'my-category-slug', 'posts_per_page' => -1 ) );
if ( have_posts() ) : while ( have_posts() ) : the_post();
// YOUR STUFF LIKE the_title(); or the_content();
endwhile; endif;

This is NOT a fix to your code, but it answers the question you asked. I think your problem may be in the use of $args inside the loop (seems odd), but if you want me to make sure I might need more of the code or a working example I can see.

http://codex.wordpress.org/Function_Reference/query_posts#All_Posts_in_a_Category

AHEM...yeah... I'm an idiot... don't go pasting this around. Use WP_Query!! thanks Pieter.

tempranova
  • 929
  • 9
  • 13
  • yes. it's works. but i want to show result in array. foreach ( $myposts as $post ) : setup_postdata( $post ); $result = array( "id"=>$args['ID'], "type"=>$args['post_type'], "url"=>$args['guid']); endforeach; this section is correct or not? [ID, post_type, guid's are column name of WP_posts].is it possible to show like this? – Guna Jul 25 '14 at 08:18
  • I might be confused... what unique information are you trying to store in the array? You are calling $args, which is ('category=news&posts_per_page=-1'), and not any kind of loop command that might store a variable you want... such as getting the ID (http://codex.wordpress.org/Function_Reference/get_the_ID), getting the post type (http://codex.wordpress.org/Function_Reference/get_post_type), or the link (http://codex.wordpress.org/Function_Reference/get_permalink). I think $args might be throwing an error with its odd formatting in the array? I am not a PHP expert. – tempranova Jul 25 '14 at 08:30
  • 1
    `query_posts` should **NEVER** be used. If you need a custom query, use `WP_Query` or `get_posts`. If you need to alter the main query, use `pre_get_posts` – Pieter Goosen Jul 25 '14 at 09:08
1

If you want to display posts from a specific category in the category page, you can use the below given code in your theme's category.php file.

<?php
    if(have_posts()) :
        while (have_posts()) : the_post();
?>
            <a href="<?php the_permalink();?>"><?php the_title();?></a>
            <?php
                the_post_thumbnail();
                the_content();    
            ?>
<?php
        endwhile;        
    endif;
?>

If you want to display the same in pages other than category page, just add the following code to the corresponding files.

<?php
    $posts = new WP_Query();
    $posts->query( "category_name='{enter your category slug here}'&posts_per_page=-1" );
    if($posts->have_posts()) :
        while ($posts->have_posts()) : $posts->the_post();
?>
            <a href="<?php the_permalink();?>"><?php the_title();?></a>
            <?php
                the_post_thumbnail();
                the_content();    
            ?>
<?php
        endwhile;        
    endif;
    wp_reset_postdata();
?>
Anoop Asok
  • 1,311
  • 1
  • 8
  • 20
0

On your theme directory, search category.php. If it doesn't contain, create a new file category.php and paste this code:

<?php if(have_posts()) : while (have_posts()) : the_post(); ?>
    <a href="<?php the_permalink();?>"><?php the_title();?></a>
    <?php the_excerpt();?>
<?php endwhile; else :?>
<?php endif;?>