To show the post in a page of my theme - php

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; ?>

Related

Search within a category in worpdress

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');

Am looking for a way to show random products in products category page using woocomerce

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"]' ) ?>

Custom Post Type displays post belong on the category

I have a custom post type advice and taxonomy adcat. I want to display all post belong to that category.
Lets say I have 4 categories namely : 'games', 'tours', 'dishes', 'hotels' and also this four category is a menu. If I click one of the category for example: hotels all post belong to the hotels should display.
By the way this code I used to show wordpress default categories:
<?php $catname = wp_title('', false); ?>
<?php $posts = get_posts("category_name=$catname&numberposts=8&offset=0");
foreach ($posts as $post) : start_wp(); ?>
//html output
<h1><?php the_title(); ?></h1>
<?php endforeach; ?>
this is not working in custom post 'taxonomies' any suggestion would be helpful thank's
try this maybe it work... I write a note so you can see whats going on.. hope it help
<?php
// Get the term/category of the post
$terms = get_the_terms( $post->ID , 'advice-cat' );
foreach ( $terms as $term ) {
$term_link = get_term_link( $term, 'advice cat' );
}
//WordPress loop for custom post type
$terms = get_the_terms( $post->ID , 'advice-cat' );
$my_query = new WP_Query('post_type=advice&advice-cat=' . $term->name . '&posts_per_page=-1');
while ($my_query->have_posts()) : $my_query->the_post(); ?>
// output content
<?php the_title(); ?>
<?php endwhile; wp_reset_query(); ?>
try something like this, this is the example to fetch post by category id
$args = array( 'cat' => $cat_id, 'post_type' => 'advice', 'posts_per_page' => -1 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
the_title();
endwhile;
wp_reset_postdata();
you can also use with category name as well.
$args = array('category_name' => 'catname', 'post_type' => 'advice', 'posts_per_page' => -1 );
you can use post_per_page as per your requirement, I have just added -1 for all the posts
I don't really understand your terminology. When you are talking about adcat, is it a custom taxonomy or a term of the build-in taxonomy category. If adcat is a custom taxonomy, you should use the tax_query in WP_Query,not the category parameters.
Remember, category and a custom taxonomy are both taxonomies, their direct children is called terms, and their direct childen is called child terms
You should also not be using wp_title() to get the queries object. You should be using get_query_var() to get the queried object. For categories it will be cat, taxonomy will be taxonomy and for terms term. Have a look at get_categories, get_taxonomies and get_terms for returned values
Example
$category = get_query_var( `cat` );
$cat_slug = $category->slug;
You can now return $cat_slug to your custom query as category_name
EDIT
I've quickly rethinked the whole thing and checked your comment as well. Why not just copy your index.php and rename it taxonomy.php. There is no need at all for a custom query here. The default loop in taxonomy.php should do it
EDIT 2
For further reading, go and check the following articles
Theme Development
Template Hierarchy
Not sure if I read your answer correctly but what I am assuming is you want to search posts by the taxonomy terms correct? So in your adcat taxonomy you have 'games', 'tours', 'dishes', 'hotels' as your terms or categories. Your best option would be to use a tax_query in your query arguments. Here is how to do that with the WP_Query() object.
<?php
$args = array(
'post_type' => 'advice',
'posts_per_page' => 8,
'tax_query' => array(
'taxonomy' => 'adcat',
'terms' => array('games', 'tours', 'dishes', 'hotels'),
'field' => 'slug'
)
);
$query = new WP_Query($args);
if($query->have_posts()) : while($query->have_posts()) : $query->the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php endwhile; endif; ?>
<?php wp_reset_postdata(); ?>
Important Note:
When using a WP_Query you will be overwriting the default post data. So in order to get that data back you just use the wp_reset_postdata() as you can see in the example above after the loop has finished.

wordpress query_posts alternative

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!

Issue with accessing category for post formatting in WordPress

I am creating separate templates and I am trying to have post query based on the category but for some reason it seems not to be working. If you could give me a hand that would be great. I am just getting every single post no matter what category.
Code:
$args = array( 'numberposts' => 100000000000, 'offset'=> 1, 'category' => 'Uncategorized' );
$lastposts = get_posts( $args );
foreach($lastposts as $post) : setup_postdata($post); ?>
<!--<h2><?php //the_title(); ?></h2>-->
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endforeach; ?>
Read get_post you will see many note over there one of which says
Note: The category parameter needs to be the ID of the category, and not the category name.
So give category ID for your category "Uncategorized".

Categories