How to filter by ACF fields in WP loop?
<?php
$company = the_field('company');
$loop = new WP_Query( array(
'post_type' => 'jobs',
'posts_per_page' => '100',
'meta_key' => 'company',
'meta_value' => the_field('company')
)
);
?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php get_template_part( 'components/loop' );?>
<?php endwhile; wp_reset_query(); ?>
If the meta_value is a string it works. Wondering how to make this work dynamically.
You need to use get_field instead of the_field. You can print custom-filed values with the get_field function.
$loop = new WP_Query(
array(
'post_type' => 'jobs',
'posts_per_page' => '100',
'meta_key' => 'company',
'meta_value' => get_field('company'),
)
);
?>
<?php while ($loop->have_posts()): $loop->the_post();?>
<?php get_template_part('components/loop');?>
<?php endwhile;
wp_reset_query();?>
Related
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 );
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.
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.
I have a taxonomy called portfolio_cat with its categories.So now i need to create a slider with that categories as a title and their post items.How i can do that? What Wordpress loop i need to have so i could put in a slider Wordpress categories with their posts?
I dont know how to customize this loop to fit in
<?php
$query = new WP_Query( array('post_type' => 'portfolio', 'posts_per_page' => 7, 'order' => ASC ) );
while ( $query->have_posts() ) : $query->the_post();
?>
I think you need this:
$portfolioArgs = array(
'order' => 'ASC',
'post_type' => 'portfolio',
'posts_per_page' => 7,
'tax_query' => array(
array(
'taxonomy' => 'portfolio_cat',
'field' => 'slug'
)
)
);
$query = new WP_Query( $portfolioArgs );
while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="slide">
<?php the_title( '<h2>', '</h2>' ); ?>
<?php the_excerpt(); ?>
</div>
<?php
endwhile;
?>
I have a wordpress blog that has a theme with some custom code to access posts by category. I've not used wordpress until now, and would like to make this code work for pages instead of posts. So far I've tried a few basic queries from codex.wordpress.org without much success.
Can anyone assist with this please?
<?php $some_array_of_pages = new WP_Query( array( 'ignore_sticky_posts' => 1, 'posts_per_page' => -1, 'orderby' => 'title', 'order' => 'ASC', 'category_name' => 'category' ) ); ?>
<?php while ($some_array_of_pages->have_posts()) : ?>
<?php $properties->the_post(); ?>
<?php $meta1 = get_post_meta( get_the_ID(), 'meta_name', true ); ?>
<?php $meta2 = get_post_meta( get_the_ID(), 'meta_name', true ); ?>
<?php if ($meta1 && $meta2): ?>
//do something here
<?php endif; ?>
<?php wp_reset_postdata(); ?>
<?php endwhile; ?>
Add 'post_type' to your query:
<?php $some_array_of_pages = new WP_Query( array( 'post_type'=>'page' ) ); ?>
http://codex.wordpress.org/Class_Reference/WP_Query
Full example from OP:
<?php $some_array_of_pages = new WP_Query( array( 'ignore_sticky_posts' => 1, 'posts_per_page' => -1, 'orderby' => 'title', 'order' => 'ASC', 'category_name' => 'category', 'post_type' => 'page' ) ); ?>