<?php
$args = array(
'posts_per_page' => 4,
'post_type' => 'products',
'orderby' => 'menu_order',
'order' => 'ASC'
);
$the_query = new WP_Query( $args );
while ($the_query -> have_posts()) : $the_query -> the_post();
?>
<div class="product-list-container <?php echo implode(' ,', wp_get_post_tags( get_the_ID(), array('fields' => 'names') ) ); ?>">
<a href="<?php the_permalink() ?>">
<div class="img-place"><img src="<?php echo get_the_post_thumbnail_url( get_the_ID(), 'full' ); ?>" alt="<?php the_title(); ?>" /></div>
<span><?php the_title(); ?></span>
* Display review stars here *
</a>
</div>
<?php endwhile;
wp_reset_postdata();
?>
]]>Use case: I want to combine that power to filter posts using a dynamic variable (populated from PHP) with a custom meta value. For example; Filter all custom post types if the meta value on the WP User object matches with a meta value one or more posts. I want to do this in a secure way so that folks cannot change the author-id in the browser editor to see other folks posts.
It is not clear how one could use stored meta values in the FSE (code view) or in visual block mode. The idea is that sometimes one needs to access stored meta for a user and provide that as a filter on a query block. There seems no way to connect a variable found in dynamic data (PHP) in a specific block enabled page. However the query block appears to support various hardcoded attributes in the FSE.
Since the page has blocks everywhere I don’t expect that I could drop into PHP to grab my user meta value?
Alternatives might be if I could set the query in code and then it would filter as needed using some dynamic data (PHP?). Not sure of WPQuery behavior. Could I load a query before the page is presented and have the posts use the active query?
]]>$keyword = sanitize_text_field($request['keyword']);
$args = array(
'post_type' => 'work',
'posts_per_page' => 20,
's' => $keyword,
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'work_category',
'field' => 'name',
'terms' => $keyword,
'operator' => 'LIKE',
),
array(
'taxonomy' => 'work_style',
'field' => 'name',
'terms' => $keyword,
'operator' => 'LIKE',
),
array(
'taxonomy' => 'work_material',
'field' => 'name',
'terms' => $keyword,
'operator' => 'LIKE',
),
),
);
$query = new WP_Query($args);
]]>Luckily this argument can be changed by the built-in filter, so it’s not a big deal. So if you want to be able to filter by taxonomy in the query block, you will need to add something like this.
function wp_docs_shadow_taxonomy_args( $args, $post_type ) {
// Target your post type
if ( 'your_post_type' !== $post_type ) {
return $args;
}
// Set as publicly queryable
$args['publicly_queryable'] = true;
// Return
return $args;
}
add_filter( 'shadow_terms_register_taxonomy_args', 'wp_docs_shadow_taxonomy_args', 10, 2 );
]]>// Query Arguments to order posts by Group, Rank, and Seniority
$args = array(
'post_type' => 'personnel ',
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'AND',
'group_clause' => array (
'key' => 'group',
'value' => 'Resigned/Retired/LODD',
'compare' => '!=',
),
'rank_clause' => array(
'key' => 'rank',
'compare' => 'EXISTS',
),
),
'orderby' => array(
'group_clause' => 'ASC',
'rank_clause' => 'DESC',
'date' => 'ASC',
),
);
// Query the database
$loop = new WP_Query( $args );
I’ve tried adding promotion_clause => array but I need to include all the posts – even those without a promotion date. Is there a way to order by promotion date first if it exists, then by post date?
Thanks in advance
]]>In my wp query I don’t know how to deal with it. On “site.com/person/max” I would like to show all books that habe the term “max”(id: 10) in the field “published”. I tried two different ways. Both don’t work.
$args = [
'posts_per_page' => -1,
'meta_query' => [],
'post_type' => 'book',
'order' => 'DESC',
'orderby'=> 'date',
'tax_query' => array(
array (
'taxonomy' => 'published',
'field' => 'term_id',
'terms' => '10',
)
),
];
'meta_query' => array( array(
'key' => 'published',
'value' => '10',
'compare' => 'IN',
'type' => 'term_id',
))
The first one shows all books, the second one just the books that ONLY have the term_id 10, not those which also have other publishers.
]]>one half — displaying columns — works fine:
// Create a meta query to filter posts by the custom field "is_column"
$meta_query = array(
? ? array(
? ? ? 'key' => 'is_column',
? ? ? 'value' => '1',
? ? ? 'compare' => '=='
? ? )
? );
? // Create an array of arguments
? $args = array(
? ? 'post_type' => 'post', // Post type
? ? 'posts_per_page' => 4, // Number of posts to return
? ? 'meta_query' => $meta_query, // The meta query created above
? ? 'orderby' => 'post_date', // Order by post date
? ? 'order' => 'DESC', // Order from newest to oldest
? ? 'ignore_sticky_posts' => false // Include sticky posts at the top
? );
? // Create a new WP_Query object with the arguments
? $query = new WP_Query($args);
? // Check if the query has any posts
? if ($query->have_posts()) {
? ? // Loop through the posts
? ? while ($query->have_posts()) {
? ? ? // Set up the post data
? ? ? $query->the_post();
? ? ? // Display the post title
? ? ? echo '<a href="'. get_the_permalink() .'">';
? ? ? the_title(); // Change this from the_content() to the_title()
? ? ? echo '</a>';
? ? }
? ? // Restore the original post data
? ? wp_reset_postdata();
? } else {
? ? // No posts found
? ? echo 'No posts found with the custom field "is_column" set to true.';
? }
However, when I invert the query like this:
$meta_query = array(
? ? array(
? ? ? 'key' => 'is_column',
? ? ? 'value' => '1',
? ? ? 'compare' => '!='
? ? )
? );
or even like this:
$meta_query = array(
? ? array(
? ? ? 'key' => 'is_column',
? ? ? 'value' => NULL,
? ? ? 'compare' => '=='
? ? )
? );
…it falls back to ‘No posts found’.
How can I run this query?
I am using
PHP 8.1
WP 6.4.2
Event Calendar – 6.2.9