I'm trying to search within a single category on wordpress using the code below
`
$args = new WP_Query( 'cat=199' );
if( $args->have_posts() ){
while ( $args->have_posts() ){ $args->the_post();
get_template_part('content', 'what-we-eat');
} ?>
<?php
}
else{
_e( 'Sorry, no posts matched your criteria.' );
}
`
however, the results shows all of the posts within that category even though the search term updates with the search term used.
Any ideas where i'm going wrong?
EDIT
I've noticed then when I remove my custom query it works, but pulls in posts from all of the categories on my blog which is what I don't want. So it's a question of excluding all other categories
Try this
<?php
global $post;
$args = array( 'posts_per_page' => 5, 'offset'=> 1, 'category' => 199 );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<li>
<?php the_title(); ?>
</li>
<?php endforeach;
?>
You want standard WP search to work in only one category?
Try to add this code to the functions.php of your theme:
function search_rules($query) {
if ($query->is_search) {
$query->set('cat','199');
}
return $query;
}
add_filter('pre_get_posts','search_rules');
Related
I have a wp_query getting all posts in a category with the same slug as the title as the current page.
Where i have become stuck is modifying wp_query category_name => $post->post_name plus a text string.
For example if page was to be called "Long Day" all posts with the category slug "long-day" will be shown. I need to bring in these posts in one loop and in another have post_name plus the text sting eg long-day-today.
This is what i have so far...
<?php
global
$post; $args = array(
'category_name' => $post->post_name
);
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="entry"><?php the_content(); ?></div>
<?php echo $cat ?>
<?php endwhile; else: ?>
<p>Sorry, there are no posts to display</p>
<?php endif; ?>
[EDIT]
I feel like a bit of a fool now, after a little playing i came up with this
global
$post;
$exclude = "hello";
$args = array(
'category_name' => "{$post->post_name} . $exclude"
);
Which seems to do the trick, if there is a better way of achieving this i would still be interested to hear...
Thanks
global
$post;
$exclude = "hello";
$args = array(
'category_name' => "{$post->post_name} . $exclude"
);
How do I change product in same category randomly? I've been looking but can't seem to find any plugin/script to do this, anyone got an idea on this... thanks
You can use the following code to display the products rows of "CATXXX" :
<?php echo do_shortcode( '[product_category category="CATXXX" per_page="8" columns="4" orderby="rand"]' ) ?>
Also you can treat products like any other post type and query using get_posts(). Limit the number of posts retrieved to 1 and change the orderby parameter to random:
$args = array(
'posts_per_page' => 1,
'orderby' => 'rand',
'category' => 'CATXXX'
'post_type' => 'product');
$random_products = get_posts( $args );
foreach ( $random_products as $post ) : setup_postdata( $post ); ?>
<li>
<?php the_title(); ?>
</li>
<?php endforeach;
wp_reset_postdata();
You can also use new WP_Query() which would be similar.
You can try this code shortcode
[product_category category="category_slug" per_page="10" orderby="rand"]
Or
<?php echo do_shortcode( '[product_category category="category_slug" per_page="10" orderby="rand"]' ) ?>
What i'm trying to do is find out what category the Posts are when they are returned so i can put them in specific divs.
e.g. i have #div1 #div2 and #div3 and i have 3 posts returned each with a different category.
What i want to do is have 1 post each in a div. To do this i need to determine the category.
Here is how i fetch Posts by category but how do i fetch Posts of 3 categories and determine the category of the Posts returned, so i can change the div id?
<?php
//gallery
$args = array( 'category_name' => 'Clubs');
$postslist = get_posts( $args );
foreach ( $postslist as $post ) :
setup_postdata( $post );
?>
<div id="div1">
<?php the_content(); ?>
</div>
<?php
endforeach;
wp_reset_postdata();
?>
Obviously i could duplicate this code twice and just change the category and div names but is that the right way to do this?
To avoid copy-paste, I'd suggest putting your code in a function, which takes the parts you want to change as parameters. Something like this (I added the posts_per_page parameter, since you said you only want one post per div):
<?php
function show_gallery($category, $id) {
$args = array( 'category_name' => $category, 'posts_per_page' => 1);
$postslist = get_posts( $args );
foreach ( $postslist as $post ) :
setup_postdata( $post );
?>
<div id="div<?php echo $id; ?>">
<?php the_content(); ?>
</div>
<?php
endforeach;
wp_reset_postdata();
}
show_gallery('Clubs', 1);
show_gallery('Hearts', 2);
show_gallery('Diamonds', 3);
?>
I haven't tested, but hopefully it works and is enough to get you started.
I'd put the function declaration at the top of your template.
I am creating a website that integrates a portfolio which uses custom post types, this was done based off of this tutorial.
So far it is exactly what I am looking for and works great except for one small detail. In order to fetch the posts from the new custom post type the author of the tutorial used the query_posts() codex. So the top of my portfolio page looks like this:
<?php
/* Template Name: Portfolio */
get_header();
query_posts('post_type=portfolio&posts_per_page=10');
?>
What I gather is that this declares "get posts from "post type" portfolio and show 10 per page". My problem is that I can't go get content from my portfolio page. It seems that now my portfolio page only fetches the content from the custom post type, and I can't use:
<?php while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; // end of the loop. ?>
to get content from the actual page.
This is what I am trying to do, I've replaced:
query_posts('post_type=portfolio&posts_per_page=10');
with:
add_action( 'pre_get_posts', 'add_my_post_types_to_query' );
function add_my_post_types_to_query( $query ) {
if ( is_page( 8 ) && $query->is_main_query() )
$query->set( 'post_type', array( 'portfolio' ) );
return $query;
}
This seems like the right track, but it stills doesn't work. I'm not getting the posts from my custom post type.
Any ideas how I could modify this? I am also still learning so being clear with explanations would be greatly appreciated.
Thank you!
Editing the pre_get_posts will replace the original query and you will not have the content for your page at all. I would only recommend going this approach if you only wanted to display the content of your portfolio post type and not the content of your portfolio page.
For general post queries it is recommended to use WP_Query or get_posts.
http://codex.wordpress.org/Class_Reference/WP_Query
http://codex.wordpress.org/Template_Tags/get_posts
If you use the WP_Query function the wp_reset_postdata() will restore the post data back to the original so you can get the content of your original page.
$args = array(
'posts_per_page' => 10,
'post_type' => 'portfolio',
);
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
Now you will be able to use the original loop to show the content of your page
<?php while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; // end of the loop. ?>
Usually, I stick my query posts in a variable, like so:
$catid = get_cat_ID('My Category Name');
$args = array(
'posts_per_page' => 5,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'category' => $catid
);
$posts_array = get_posts($args);
Then you can loop it like so:
<?php foreach ($posts_array as $post) : setup_postdata($post);?>
<h1><?php the_title(); ?></h1>
<p><?php the_content(); ?></p>
<?php endforeach; ?>
Finally, to access your page content you can use the variable $post, it's automatically set by wordpress. No need to add any more code than this to access your page content.
<?php foreach( $posts as $post ) : setup_postdata($post); ?>
<h1><?php the_title(); ?></h1>
<p><?php the_content(); ?></p>
<?php endforeach; ?>
The foreach loop for your page content is a little overkill, and there is a better way to do it (most likely at least), but I haven't been bothered to look into it further yet! It works though!
I have to show all the posts from custom type post on my product page. So I am using the Custom Post Type plugin. Now I want to display all the posts in the product page like it was a blog page of WordPress. I am using the code below to display the posts in the product page..
add_filter( 'pre_get_posts', 'my_get_posts' );
function my_get_posts( $query ) {
if ( is_page('product') && $query->is_main_query() )
$query->set( 'post_type', array('products' ) );
return $query;
}
But I can't get the page. If there is some other way to display all the posts in the page, then please help me out.
It should be easier to make a specific template for your page, and use get_posts function.
To create a specific template for a page :
http://codex.wordpress.org/Pages#What_Template_is_Used_to_Display_a_Particular_Page.3F
How to use get_posts :
http://codex.wordpress.org/Template_Tags/get_posts
Add this code where you want all post's change the ID of category and number of post's...
<?php
global $post;
$args = array( 'numberposts' => 5, 'category' => 3 );
$myposts = get_posts( $args );
foreach( $myposts as $post ) :
setup_postdata($post); ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php endforeach; ?>