Wordpress select custom post by metabox - php

So I have a custom post called event, but I need to query only those posts which have a custom metabox(is a checkbox) called `Show On The Homepage: and only if that checkbox is checked.
<?php
$args = array( 'post_type' => 'Event','posts_per_page' => 1000 );
$loop = new WP_Query( $args );
?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
My question is there a way to query this, or I will have to do a get_meta in the loop and check each one with an if statement

I'll go ahead and answer this anyway
Your answer is here: http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
So use meta_key and meta_value in your args

Related

Custom Post with Products

i am trying to get all details of my custom post and print selected data from that on my single-product page.
so lets say the custom post is named Games and the product page is the one were you select on of the games to buy.
i am using:
$args = array(
'post_type' => 'games',
)
);
$review_details = new WP_Query($args);
this is getting me most of the information the product instance, although i chose the post type to be games. since in the post i have the age and rating for the games.
How will I be able to get all the details i already have in the Games Post to the single-product page of a game i am selling?
$args = array(
'post_type' => 'reviews',
'meta_query' => array(
array(
'key' => 'games_books', // name of custom field
'value' => '"' . $game_id . '"', // matches exaclty "123", not just 123. This prevents a match for "1234"
'compare' => 'LIKE'
)
));
$review_details = new WP_Query($args);
print_r($review_details);
and when i use print_r($review_details); all the parameters of reviews are empty, but when i do the same from a post then i can get the reviews.
again i need to print this data on the single-product page
It sounds like you have additional meta information for the Custom Post Type 'games'. Outputting this extra information can be done using either:
get_post_meta($post_id, 'key');
See here for more information https://developer.wordpress.org/reference/functions/get_post_meta/
Or if you're using a common custom field plugin, like ACF, you'll need to use get_field()
get_field('key', $post_id);
See here for more info https://www.advancedcustomfields.com/resources/get_field/
To print a data for custom post, you can use WP_Query with While Option.
For example:
<?php
$args = array( 'post_type' => 'games');
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<!-- Loop Start -->
<?php the_title(); ?>
<!-- Loop End -->
<?php else: endif; ?>
If you want to print custom areas
<?php echo get_post_meta($post->ID, 'key', true); ?>

How to fetch all the woocommerce products from the database using php

I am trying to fetch all the products of the woocommerce from the database using php.. But it is not giving me the list of all products
<select Name='choose'>
<?php
$args = array( 'post_type' => 'product' ,'posts_per_page' => 100);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) :
$loop->the_post();
echo '<option selected value="'.the_title().'</option>';
endwhile;
?>
</select>
can anybody please tell me how can I get all the products of woo commerce
Thanks
In order to fetch Single product to your single.php page you can make use of the code which i have already suggested over here. Refer here:
https://wordpress.stackexchange.com/questions/240653/how-to-query-single-product-in-woocommerce/240726#240726
In order to fetch All products that you have into your DB you can follow the steps below.
You can fetch the Woo-commerce Products based on the WP_Query also so that it is also saved in the wp_posts table alone. So that we might slightly modify the post type alone for getting the WP_Query to work on.
Step One:
First We shall see to the parameters what we have to pass for the WP_Query.
$params = array(
'posts_per_page' => 5,
'post_type' => 'product'
);
Note: As you can see, all we’ve done is added a post_type variable to the array, and set it’s value to "product"; the query will now look for WooCommerce products instead of posts.
Step Two: After that we have to run on with the same structure that the Normal WP_Query behaves.
<?php
$params = array(
'posts_per_page' => 5,
'post_type' => 'product'
);
$wc_query = new WP_Query($params);
?>
<?php if ($wc_query->have_posts()) : ?>
<?php while ($wc_query->have_posts()) : $wc_query->the_post(); ?>
<?php the_title(); //Prints the title over Here ?>
<?php //You can add what ever you need from the loop over here ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p>
<?php _e( 'No Products' ); ?>
</p>
<?php endif; ?>
Explanations
An array of parameters for WP_Query to work with is created; to start with it’s just the basic posts, but by adding more specifics to these parameters we can achieve different results for our queries.
WP_Query is used to query the parameters created in the first line.
The query is checked to see if it returned any results.
If there are results, then we iterate over them:
First, setting the global variable $post, which ensures that the next function works.
Second, setting the the_title() variable, which is responsible for displaying the product title.
Then, when the posts are displayed, we return the $post variable to its original state with thewp_reset_postdata function.
Using this link i have studied how to query the WooCommerce Products using WP_Query and I have shared here so that all might get used with this posts.
Happy Coding :)
Your code is working fine on my machine, However its seems some conflict to your previous wp query. You can also try to get all product like this..
<select Name='choose'>
<?php
$args = array( 'post_type' => 'product' ,'posts_per_page' => 100);
$products = get_posts( $args );
foreach( $products as $product ) :
echo '<option selected value="'.$product->post_title.'</option>';
endforeach;
?>
</select>
$this->db->select('*');
$this->db->from('vm_posts');
$this->db->join('vm_postmeta', 'vm_postmeta.post_id = vm_posts.id');
$this->db->join('vm_wc_product_meta_lookup', 'vm_wc_product_meta_lookup.product_id = vm_postmeta.post_id');
$this->db->join('vm_inventory', 'vm_wc_product_meta_lookup.sku = vm_inventory.product_sku');
$this->db->join('vm_terms', 'vm_inventory.customer_id = vm_terms.name');
$this->db->where('post_type','product');
$query = $this->db->get()->result();

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.

Fetch and display Wordpress category of custom post type

I am currently working on a personal project and the this page basically has two tabs each will display the archive for specific categories under one custom post type called webinar.
I am calling the category in one of the tabs using
<?php query_posts('category_name=demos-on-demand-videos'); ?>
However when i do this i' just getting the no post's found screen, what am i doing wrong? I am trying to display the post archive from the category demos-on-demand-videos which is under the webinar custom post type.
use this
query_posts( array( 'post_type' => 'webinar','your-custom-taxnomy' => 'demos-on-demand-videos' ) );
while ( have_posts() ) :
the_post();
$post_id = $post->ID;
endwhile;
Follow this link:
http://eyan16.wordpress.com/2013/09/16/how-to-fetch-posts-from-custom-posts-type-with-custom-taxonomy/
Make a page template for each of your tabs and use this custom loop in it. Make sure to adjust it for your specific post type, taxonomy, or term.
<?php $args=array(
'post_type' => 'webinar', //set the post_type to use.
'taxonomy' => 'demos-on-demand-videos', // set the taxonomy to use.
'term' => 'term1', //set which term to use or comment out if not using.
'posts_per_page' => 10 // how many posts or comment out for all.
);
$webinarloop = new WP_Query($args);
if($webinarloop->have_posts()) : while($webinarloop->have_posts()) :
$webinarloop->the_post();
get_template_part( 'content' ); //or whatever method you use for displaying your content.
endwhile; endif; //end the custom post_type loop
?>
This code works for me just fine
* x is taxonomy name name you have created
* y is category slug
$args = array('post_type' => 'client','posts_per_page'=>'8','order'=>'DESC','x'=>'y');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();

Fetching the particular post having post type page

I'm facing some problem when I fetch page content of particular Id. I'm using the code given below.
There is many pages in my database, I just want to show the page having id =30 and post_type=page. When I'm using the code given below it show all page content having post_type=page. But I just only the one. I think there is some syntax problem in my code.
<?php
$loop = new WP_Query( array( 'post_type' => 'page','ID'=> 30) );
while ( $loop->have_posts() ) : $loop->the_post();
the_content();
endwhile;
?>
Try this
global $wpdb;
$details = $wpdb->get_var("SELECT * FROM $wpdb->posts WHERE ID=30 and post_type='page'");
return $details;
or
<?php
$args=array(
'post_type'=>'page',
'post__in' => array('595', '33', 44)
);
$the_query = new WP_Query($args);
?>
Good luck
Errm, what happened to good old get_post(30)?

Categories