Getting posts from categories array - wordpress

I have some certain categories' ids. I want to loop this categories and last 3 posts in one time. I try this but only come one category from array.
<?php
$args = array(
'cat' => 48,43,49,46,47,44,51,50,42,
'order' => 'ASC',
'showposts' => 3
);
query_posts($args);
?>
<?php while (have_posts()) : the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>

This piece of code won't work: 'cat' => 48,43,49,46,47,44,51,50,42,
You'll have to use an array 'cat' => array(48,43,49,46,47,44,51,50,42),

For some reason 'cat' did not work. We used
'category__in' => array( 2, 6 ),
and it workined fine.
The completed working code:
<?php
// -----------------------------
$args = array(
'post_type' => 'post',
'order' => 'ASC',
'category__in' => array(2,6)
);
$query = new WP_Query( $args );
?>

You can get All Posts in a Category which one you want publish.
query_posts( array ( 'category_name' => 'my-category-slug', 'posts_per_page' => -1 ) );
You can find post as per your expectation by.
query_posts( array ( 'category_name' => 'carousel', 'posts_per_page' => 10, 'orderby' => 'date', 'order' => 'DESC' ) );

According to your code. Update --
<?php
$args = array(
'cat' => [48,43,49,46,47,44,51,50,42], //change here array
'order' => 'ASC',
'posts_per_page' => 3 //showposts deprecated now
);
query_posts($args);
?>
<?php while (have_posts()) : the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>
<?php wp_reset_query(); ?> // you should reset your query

should actually be:
'cat' => '48,43,49,46,47,44,51,50,42'

Related

Get all Posts If has same custom field values in Posts

Trying to get all the posts having the same zipcode as metavalue. Thanks in advance for the help.
<?php
$query = new WP_Query( array(
'post_type'=> array('service'),
'posts_per_page' => -1,
'meta_query' => array( array(
'key'=> 'zipcode',
'value'=> ','.$zip.',',
'compare'=> 'LIKE'
) )
));
?>
<?php if ( $query->have_posts() ) :while ( $query->have_posts() ) : $query->the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php endwhile; // end of the loop. ?>
<?php wp_reset_query(); ?>
<?php else: ?>
No results found.
<?php endif; ?>
zipcode are numbers for example 12345. If posts have value 12345 in the custom field. then it should display all posts which have the 12345 value. The above code is working fine but displays only one post.
Following code will be the proper for the meta query.
$query_args = array(
'post_type' => 'service',
'posts_per_page' => -1,
'meta_query' => array(
array(
'value' => $zip,
'compare' => 'LIKE',
'key' => 'zipcode',
),
)
);
$query = new WP_Query($query_args);
<?php if ( $query->have_posts() ) :while ( $query->have_posts() ) : $query->the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php endwhile; // end of the loop. ?>
<?php wp_reset_query(); ?>
<?php else: ?>
No results found.
<?php endif; ?>
Hope it helps.
This does the job for me:
$popularCourses = new WP_Query(
array(
'post_type' => 'courses',
'posts_per_page' => -1,
'post_status' => 'publish',
'meta_key' => 'course-is-promoted',
'meta_value' => 'Yes',
'orderby' => 'date',
'order' => 'DESC'
)
);
There are two ways.
After watching this code i will suggest you just visit this link for your better understanding.
(1)
$args = array(
'meta_query' => array(
array(
'key' => 'Your_key',//Enter your meta key here
'value' => 'professionnel',//Enter you meta value
'compare' => '=',//Comparison type (option filed) .
)
)
);
$query = new WP_Query($args);
(2)
$output_loop = get_posts( array(
'meta_key' => 'Your_key',//Meta key
'meta_value' => 'Your_value',//Meta value
) );
Now just print_r($output_loop) for the better understanding.

get custom woocommerce product

i want to get 10 product from a category in woocommerce
for example, for get latest post of a posts category i use the following code
<?php $posts = get_posts( 'category=17&numberposts=5' ); ?>
<?php if( $posts ) : ?>
<ul>
<?php foreach( $posts as $post ) : setup_postdata( $post ); ?>
<li><i class="circle"></i><?php the_title(); ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
i want a code, like this for get woocommerce products
try this example :
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => '12',
'meta_query' => array(
array(
'key' => '_visibility',
'value' => array('catalog', 'visible'),
'compare' => 'IN'
)
),
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id', //This is optional, as it defaults to 'term_id'
'terms' => 26,
'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
)
)
);
$products = new WP_Query($args);
/* your loop */
Hope this will helps you.
Try this example,
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => 10,
'product_cat' => 'hoodies'
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
echo '<br />' . woocommerce_get_product_thumbnail().' '.get_the_title().'';
endwhile;
wp_reset_query();
?>
OR
<?php
$args = array( 'post_type' => 'product', 'category' => 34, 'posts_per_page' => -1 );
$products = get_posts( $args );
?>
Hope this will helps you.
For more details please visit,
Woocommerce get products
why not use woocommerce's product_category shortcode?
<?php echo do_shortcode("[product_category category='17' limit='5']"); ?>
It will list the products same as in shop page. With these you are safe with product attributes like if when it's out of stock.

How to display all posts in WordPress?

I want to display all post in my WordPress home page.
I have written following query for getting all post but I do not get all posts. It just displays 10 or 11 posts:
$args = array(
'post_type' => 'post',
'posts_per_page' => $number,
'order' => $sort_by,
'orderby' => 'title',
'post_status' => 'publish',
'tag' => $tags,
'ignore_sticky_posts' => 1,
);
$args['tax_query'] = array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => 'post-format-video',
));
$query = new WP_Query($args);
So please let me know how can I get all posts.
Displaying all posts that has been published. You have to use post_per_page='-1' to retrive all the posts.
$args = array(
'post_type'=> 'post',
'orderby' => 'ID',
'post_status' => 'publish',
'order' => 'DESC',
'posts_per_page' => -1 // this will retrive all the post that is published
);
$result = new WP_Query( $args );
if ( $result-> have_posts() ) : ?>
<?php while ( $result->have_posts() ) : $result->the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>
<?php endif; wp_reset_postdata(); ?>
Hope so this will retrive all the posts as per your expectation.
try Multiple loops :
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<!-- do stuff ... -->
<?php endwhile; ?>
<?php endif; ?>
if you need Advanceded query use this :
<?php query_posts( 'post_type=post&posts_per_page=10'); ?>
and you can use query_posts() get all posts
Set the variable posts_per_page => -1
As so:
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'order' => $sort_by,
'orderby' => 'title',
'post_status' => 'publish',
'tag' => $tags,
'ignore_sticky_posts' => 1,
);
This will make Query get all posts in your table.
See more in the WP Query docs

How to create a bxslider with wordpress post type categories

I have a taxonomy called portfolio_cat with its categories.So now i need to create a slider with that categories as a title and their post items.How i can do that? What Wordpress loop i need to have so i could put in a slider Wordpress categories with their posts?
I dont know how to customize this loop to fit in
<?php
$query = new WP_Query( array('post_type' => 'portfolio', 'posts_per_page' => 7, 'order' => ASC ) );
while ( $query->have_posts() ) : $query->the_post();
?>
I think you need this:
$portfolioArgs = array(
'order' => 'ASC',
'post_type' => 'portfolio',
'posts_per_page' => 7,
'tax_query' => array(
array(
'taxonomy' => 'portfolio_cat',
'field' => 'slug'
)
)
);
$query = new WP_Query( $portfolioArgs );
while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="slide">
<?php the_title( '<h2>', '</h2>' ); ?>
<?php the_excerpt(); ?>
</div>
<?php
endwhile;
?>

Wordpress custom query missing two posts

I am running a custom query but when returning posts_per_page, it is always two behind. I am using a custom post type called events, that is pulling in the code to display the posts from the functions file. I also want to implement pagination(not working also), will this be a problem?
I have ran var dump which returns everything in the query including posts before the current date. I imagine because it's dumping before the if statement? Any ideas?
Any ideas?
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
//Current exhibitions
$args = array(
'post_type' => 'exhibitions',
'orderby' => 'exhibition_start_date',
'meta_key' => 'exhibition_start_date',
'order' => 'ASC',
'posts_per_page' => 6,
'paged' => $paged
);
$my_query = new WP_Query( $args );
while ( $my_query->have_posts() ) : $my_query->the_post();
$exhibition_start_date = get_post_meta($post->ID, 'exhibition_start_date', true);
//Current
if ($exhibition_start_date >= date('Y-m-d') ) {
get_exhibition_container();
}
endwhile;
wp_reset_postdata();
?>
New code: (I've also added in my initial loop which brings in the page content, thought it could interfear?)
<div role="main" class="clearfix">
<div class="one-column">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<?php endwhile; else: ?>
<?php endif; ?>
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
//Current exhibitions
$args = array(
'post_type' => 'exhibitions',
'meta_query' => array(
array (
'key' => 'exhibition_start_date',
'value' => date('Y-m-d'),
'compare' => '>='
)
),
'orderby' => 'exhibition_start_date',
'meta_key' => 'exhibition_start_date',
'order' => 'ASC',
'posts_per_page' => 10,
'paged' => $paged
);
$my_query = new WP_Query( $args );
while ( $my_query->have_posts() ) : $my_query->the_post();
$exhibition_start_date = get_post_meta($post->ID, 'exhibition_start_date', true);
//Current
get_exhibition_container();
endwhile;
wp_reset_postdata();
?>
<div class="previous-holder">
<?php previous_posts_link(); ?>
</div>
<div class="next-holder">
<?php next_posts_link(); ?>
</div>
Instead of using a if condition in your loop, you should use meta_query param :
$args = array(
'post_type' => 'exhibitions',
'meta_query' => array(
array(
'key' => 'exhibition_start_date',
'value' => date('Y-m-d'),
'compare' => '>='
)
),
'orderby' => 'exhibition_start_date',
'meta_key' => 'exhibition_start_date',
'order' => 'ASC',
'posts_per_page' => 6,
'paged' => $paged
);
EDIT : About pagination
previous_posts_link codex :
Prints a link to the previous set of posts within the current query.
You should try using previous_posts_link() and next_posts_link() before wp_reset_postdata()

Resources