Wordpress Custom Post Meta Query - wordpress

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

Related

Custom Post Type filtering by own taxonomies

I tried a lot, but with no success. This LOOP works fine. It shows all Custom Post Types (apps-und-tools):
<?php
$loop = new WP_Query(
array(
'post_type' => 'apps-und-tools',
'posts_per_page' => -1,
)
);
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div>
<h2><?php the_title(); ?></h2>
</div>
<?php endwhile;
wp_reset_postdata();
Now I want to filter by a custom taxonomy. First I get the custom taxonomy with get_queried_object() and put the tax_query to the array in the LOOP. At the end are some control outputs.
<?php
// getting custom taxonomy of actual post
$this_term = get_queried_object();
$term_id = $this_term->term_id;
$term_name = $this_term->name;
$term_taxonomy = $this_term->taxonomy;
$term_slug = $this_term->slug;
// Wordpress Loop
$loop = new WP_Query(
array(
'post_type' => 'apps-und-tools',
'posts_per_page' => -1,
'tax_query' => array(
array (
'taxonomy' => $term_taxonomy,
'name' => $term_name
)
)
)
);
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div>
<h2><?php the_title(); ?></h2>
</div>
<?php endwhile;
wp_reset_postdata();
//Control Output
echo 'term_id: ' . $this_term->term_id;
echo '<br>';
echo 'name: ' . $this_term->name;
echo '<br>';
echo 'taxonomy: ' . $this_term->taxonomy;
echo '<br>';
echo 'slug: ' . $this_term->slug;
?>
The result is, that the LOOP does not show any results. No CPTs are shown, but the control output is as expected. I'd just like to filter my CPTs by custom taxonomy.
Any help is greatly appreciated, many thanks in advance.
Please check this loop to retrieve CPTs.
$args = array(
'post_type' => arra('apps-und-tools'),
'tax_query' => array(
array(
'taxonomy' => $term_taxonomy,
'field' => 'slug',
'terms' => $term_slug,
),
),
);
$query = new WP_Query( $args );
$term_taxonomy = 'taxonomy_name';
$args = array(
'post_type' => arra('apps-und-tools'),
'post_status' => "publish",
'tax_query' => array(
array(
'taxonomy' => $term_taxonomy,
'field' => 'slug',
'terms' => $search_key,
),
),
);
$query = new WP_Query( $args );

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 all Posts If has same custom field values in Posts

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.

custom post type loop not working

I am trying to create a custom loop from the metadata I have entered into the posts.
<?php $args = array (
'post_type' => array( 'movies' ),
'order' => 'DESC',
'order_by' => 'get_post_meta( get_the_ID(), "released", true )',
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) :
bd_pagination();
while ( $query->have_posts() ) : $query->the_post(); ?>
I so so need help plz
You need to tweak your parameters a bit. I'm assuming the "released" is a date field? If not (eg, if it's a timestamp), use a numeric orderby instead. But this should set you on your way:
<?php
$args = array(
'post_type' => array( 'movies' ),
'orderby' => 'meta_value_date',
'meta_key' => 'released',
'meta_type' => 'DATE'
);
$query = new WP_Query( $args );

Exclude duplicated posts from wordpress custom queries

I'm building a template for a homepage which shows 4 latest posts on top and some groups of posts divided by category around the page (I'm using get_posts to perform queries).
What I'd like to do is exclude from these category posts any post already present in the latest four news on top.
I guess I should get that four post IDs and use the 'post__not_in' parameter in the "category" queries, but I can't make it work.
Dou you have any hints?
This is the code:
// First query: I get last four posts
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'numberposts' => 4
);
// In my dreams, this should be the array of post IDs
$ids->query_vars['page_id'];
// Second query: I get 5 posts for a given categoory and exclude posts with IDs from first query
$query = new WP_Query($args);
$args2 = array(
'numberposts' => 5,
'category' => 15,
'orderby' => 'post_date',
'order' => 'DESC',
'post__not_in' => $ids
);
$query2 = new WP_Query($args2);
$primaposizione = get_posts( $args2 );
foreach ( $primaposizione as $post ) : setup_postdata( $post );
... do the stuff ...
endforeach;
wp_reset_postdata();
UPDATE
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'orderby' => 'post_date',
'order' => 'DESC',
'posts_per_page' => 4
);
$query = new WP_Query($args);
$excludeID = array();
while ( $query->have_posts() ) : $query->the_post();
$excludeID = $post->ID;
endwhile;
$args = array(
'posts_per_page' => 1,
'orderby' => 'post_date',
'order' => 'DESC',
'category' => 15,
'post_type' => 'post',
'post_status' => 'publish',
'post__not_in' => array($excludeID)
);
$primaposizione = get_posts( $args );
foreach ( $primaposizione as $post ) : setup_postdata( $post );
$category = get_the_category();
$slug = $category[0]->category_nicename ;
$esteso = $category[0]->cat_name;
if(has_post_thumbnail()) { ?>
<span class="hidden-xs"><?php the_post_thumbnail('bones-thumb-300', array('class' => 'img-responsive')) ?></span>
<?php } ?>
<h3 class="ellipsis"><?php the_title(); ?></h3>
<?php the_excerpt();?>
<?php endforeach;
wp_reset_postdata();
?>
The way you're trying to select the ID's is not going to work. You have to call wp_query first. I think this should work:
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'numberposts' => 4
);
$query = new WP_Query($args);
$excludeID = array();
while ( $query->have_posts() ) : $query->the_post(); ?>
$excludeID[] = $post->ID; // forgot the brackets
// do stuff
endwhile;
(...)

Resources