I want to get more than one category with wordpress get posts function.
This is what I have:
$posts = get_posts(array(
'posts_per_page' => -1,
'category_name' => 'featured'
)
);
And I want to add the category "artworks". I tried like this but it doesn't work, any ideas how to do it?
$posts = get_posts(array(
'posts_per_page' => -1,
'category_name' => 'featured', 'artworks'
)
);
As noted in the Wordpress docs it looks like you have to pass category as a number, and I don't see 'category_name' as an option in the documentation.
It also appears that you can pass only a single category per get_posts() call.
So for a single category:
$posts = get_posts(array(
'posts_per_page' => -1,
'category' => '2'
)
);
Or for multiple
$vars = array(
array(
'posts_per_page' => -1,
'category' => '2'
),
array(
'posts_per_page' => -1,
'category' => '3'
)
);
foreach $vars as $post_array{
$posts[] = get_posts($post_array);
}
And print_r($posts); will have the resulting array.
Related
I need to customize the word press basic search filter.
It is working fine searching against keywords in post title and post content.
Now i need to show results where if user enters the name matching the
category name, then it should pull results from that category as well
as other results.
I am assuming, it should be something like using
LIKE clause for the category_name or category_in operators in tax
query.
$args = get_posts(array(
'fields' => 'ids',
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
's' => $_REQUEST['s'] ? $_REQUEST['s'] : '' ,
'tax_query' => array(
array(
'taxonomy' => 'NAME OF TAXONOMY',
'field' => 'slug',
'terms' => 'SLUG OF the TERM', // LIKE (here should be any LIKE clause etc)
),
)
));
How to achieve this scenario, means when user enters any keyword matching the category name it should pull all the results from that category along with general search results.
Example: In search bar user writes "ABC" and there is category available with name "ABC Park", then it should pull results from this category along with results having post titles and content which contain "ABC".
Okay... I come up with a solution, I've fetched all category ids from table terms with a LIKE query, then in other query passed this array as a category parameter. Here is the code.
$term_ids=array();
$cat_Args="SELECT * FROM $wpdb->terms WHERE name LIKE '%".$_REQUEST['s']."%' ";
$cats = $wpdb->get_results($cat_Args, OBJECT);
array_push($term_ids,$cats[0]->term_id);
$q1 = get_posts(array(
'fields' => 'ids',
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
's' => $_REQUEST['s'] ? $_REQUEST['s'] : ''
));
$q2 = get_posts(array(
'fields' => 'ids',
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'category' =>$term_ids
));
$unique = array_unique( array_merge( $q1, $q2 ) );
$posts = get_posts(array(
'post_type' => 'post',
'post__in' => $unique,
'posts_per_page' => -1
));
if ($posts ) :
foreach( $posts as $post ) :
//show results
endforeach;
endif;
But Still i would like a more minimal and precise way. :)
I'm collecting statistics and there I want to show all pages, posts, taxonomy categories I have.
I could display post types and pages because actually they all have some post type but can't display taxonomy categories together with them:
<?php
$excluded_ids = array(1, 5);
$postArgs = array(
'post_type' => array('page', 'products'),
'order' => 'ASC',
'orderby' => 'title',
'post__not_in' => $excluded_ids,
'posts_per_page'=> -1,
/*'taxonomy' => 'product-category'*/
);
$postList = get_posts($postArgs);
?>
Is there a way to display everything (posts,categories,pages) via single query and not multiple? Any ideas?
The get_posts method doesn't accept array as the post_type value. You should use WP_Query to query the posts.
$args = array(
'post_type' => array( 'page', 'products' )
);
$query = new WP_Query( $args );
If i understanded you correctly
$argss = array(
'post_type' => array('page', 'products'),
'order' => 'ASC',
'orderby' => 'title',
'tax_query' => array(
array(
'taxonomy' => 'your taxonomies',
'field' => 'slug',
'terms' => 'your term',
),
),
);
$the_queryy = new WP_Query( $argss );
if ( $the_queryy->have_posts() ) {
while ( $the_queryy->have_posts() ) {
$the_queryy->the_post();
// echo stuff
}
wp_reset_postdata();
}
I am trying to fetch products by category ID this way:
<?php
$args = array(
'posts_per_page' => 20,
'taxonomy' => 'product_cat',
'post_type' => 'product',
'post_status' => 'publish',
'cat' => $cat_id
);
$query = new WP_Query($args);
$posts = get_posts($args);
var_dump($posts);
?>
The $cat_id variable contains the correct category ID. I've checked it. Products are added to correct categories.
The problem is, whenever I var_dump the $posts variable I get an empty array. As soon as I remove the 'cat' keyword from the arguments I can fetch products from all categories with no problems. The only problem is the 'cat' keyword.
Am I doing something wrong?
You could try this instead:
$args = array(
'posts_per_page' => 20,
'post_type' => 'product',
'post_status' => 'publish',
'tax_query' = array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'term' => $cat_id
)
);
$query = new WP_Query($args);
var_dump($query);
I haven't test it, but it should work.
I'm trying to retrieve the posts in Wordpress that have post_id < $number , so I looked for it in the documentation, and tried this code :
$args = array(
'numberposts' => 10,
'posts_per_page' => 10,
'offset' => 0,
'orderby' => 'id',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
array(
'key' => 'post_id',
'value' => '3000',
'compare' => '<'
)
);
$query = new WP_Query( $args );
$posts = $query->get_posts();
but it didn't work (return the last 10 posts without comparing the ID), and I couldn't find an answer :(
So, could tell me please how to do it ..
And thanks in advance ..
Use the following query. it will return you array with your desired data.
global $wpdb;
$results = $wpdb->get_results( "SELECT ID, post_title FROM $wpdb->posts WHERE ID < 10;" );
I'm trying to dynamically assign the sort order (stored in $current_sort below) for a query that lists menu items.
If I hard code the sort order it works fine, however, when I try to dynamically assign the sort parameters to a string, it fails. What am I missing?
$current_sort = ", 'orderby' => 'title', 'order' => 'asc'";
$myposts = get_posts(
array(
'cat' => "$cat,-$catHidden",
'numberposts' => $my_current_count . $current_sort
));
//If I hard code the value of $current_sort it works fine
$myposts = get_posts(
array(
'cat' => "$cat,-$catHidden",
'numberposts' => $my_current_count,
'orderby' => 'title',
'order' => 'asc'));
You cannot turn a string into PHP source code. (At least you shouldn't.)
Try this:
$current_sort_order = "title";
$current_sort_direction = "asc";
$myposts = get_posts(array(
'cat' => "$cat,-$catHidden",
'numberposts' => $my_current_count,
'orderby' => $current_sort_order,
'order' => $current_sort_direction
) );
you are doing it wrong, you don't have to concate..try this:
$current_sort = "title";
$order = "asc";
$myposts = get_posts(
array(
'cat' => "$cat,-$catHidden",
'numberposts' => $my_current_count,
'orderby'=> $current_sort,
'order' => $order
));
The string concatenation on the line
'numberposts' => $my_current_count . $current_sort
is not equivalent to creating multiple array elements as in
'numberposts' => $my_current_count,
'orderby' => 'title',
'order' => 'asc'));
In the first instance, numberposts becomes a string containing information about the sort.
In the second instance, numberposts only contains the current count.
A better option may be:
$orderoption="<TITLE HERE>";
$order_dir="<SORT OPTION HERE>";
$myposts = get_posts(
array(
'cat' => "$cat,-$catHidden",
'numberposts' => $my_current_count,
'orderby' => $orderoption,
'order' => $order_dir));