Get all Posts If has same custom field values in Posts - wordpress

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.

Related

Select post with specific group - subfield value

in wordpress I have custom post type 'referenzen'. This post type has Custom fields(ACF) 'Referenzen-buildin-type' Group with subfield 'building-type' which is checkbox. I do not know how to select posts with specific building type. This is not working for me :
$posts = get_posts(array(
'meta_query' => array(
array(
'key' => 'referenzen-building-types_building-type',
'value' => '"Museen"',
'compare' => 'LIKE'
)
)
));
Any idea? Thanks
Please have a look below:
<?php
//args
$args = = array (
'numberposts' => -1,
'post_type' => 'event',
'meta_key' => 'location',
'meta_value' => 'Melbourne'
);
//query
$the_query = new WP_Query( $args );?>
<?php if( $the_query->have_posts() ): ?>
<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
/* do something */
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>
I hope it would help you out.
$posts = get_posts(array(
'meta_query' => array(
array(
'key' => 'referenzen_building_types_building_type',
'value' => 'Museen',
'compare' => 'LIKE'
)
)
));

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.

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()

Wordpress Custom Post Meta Query

I'm trying query a custom post type for each comment with the dynamic field "comment_ID." I'm using the code below. This currently shows the comment ID, which I don't want, but does not show 'paid' as I would like.
<?php
$commID = comment_ID();
$args = array( 'post_type' => 'paidbriefs', 'meta_key' => 'Comment_ID', 'meta_value' => 'echo $commID', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo 'paid';
endwhile; ?>
</p>
I'm obviously doing something wrong with echoing the $commID variable as this does not show anything. If I change this to just $commID it returns 'paid' for every comment that has a Comment_ID meta, regardless of whether it matches the actual comment ID. Does anyone know how to fix this?
I think what you will need to do is a Loop, within a loop,
first loop to generate the standard loop, this will have your post info, comments etc.
within that loop you need to declare your comment_ID;
then from there, you setup another internal loop,
using the comment_ID for your custom field,
<?php
$args = array( 'post_type' => 'paidbriefs', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$commID = comment_ID();
$innerargs = array( 'post_type' => 'paidbriefs',
'meta_key' => 'Comment_ID',
'meta_value' => $commID,
'posts_per_page' => 10 );
$innerloop = new WP_Query( $innerargs );
while ( $innerloop ->have_posts() ) : $innerloop ->the_post();
echo 'paid Comment';
endwhile;
endwhile;
?>
untested though.
hopefully this will help,
<?php
$args = array( 'post_type' => 'ait-dir-item',
'meta_query' => array(
array(
'key' => 'location',
'value' => 'annapolis'
),
array(
'key' => 'item_tags',
'value' => 'non-marine'
)
),
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => 300 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
the_title('<h3 class="entry-title">', '</h3>');
echo '<div class="entry-content">';
the_content();
echo '</div>';
endwhile;?>
you can try this one

Getting posts from categories array

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'

Resources