I'm trying to group my posts by a meta value, and can't seem to figure it out. My query below just returns "featured" posts. I would like the query to return all posts, but have the "featured" results before the rest.
Any ideas?
$args = array(
'posts_per_page' => '20',
'paged' => $current_page,
'meta_query' => array(
array(
'key' => 'tags',
'value' => 'featured',
'compare' => 'like'
)
),
'order_by' => 'date',
'order' => 'DESC'
);
Your answer is rewind_posts(). Run your query to get all the posts, run your loop and filter out all posts which is not featured, rewind the loop, rerun the loop and filter out featured posts.
Something like this will do
$args = [
// Your query arguments to get all postst
];
$q = new WP_Query( $args );
if ( $q->have_posts() ) {
while ( $q->have_posts() ) {
$q->the_post();
if ( get_post_meta( $post->ID, 'tags', true ) == 'featured' ) {
// Run your loop for featured posts
}
}
$q->rewind_posts(); // Rewind the loop to rerun it
while ( $q->have_posts() ) {
$q->the_post();
if ( get_post_meta( $post->ID, 'tags', true ) != 'featured' ) {
// Run your loop for non featured posts
}
}
wp_reset_postdata();
}
Related
I got a shortcode to print out the brand name connected to the current page:
add_shortcode( 'MARKE', 'marke_shortcode' );
function marke_shortcode() {
$terms = array(
'post_type' => 'fahrzeuge',
'taxonomy' => 'marken',
'hide_empty' => false,
'fields' => 'ids'
);
if( is_tax('marken') ) {
$terms['tax_query'] = array(
array(
'taxonomy' => 'marken',
'field' => 'term_id',
'terms' => (array) get_queried_object_id(),
)
);
}
$posts = get_posts( $terms );
if( ! empty( $posts ) )
return get_field( 'marken', $posts[0] );
return '0';
}
Unfortunately the code isn't working.
What I try to achieve is that the brand name, e.g. Tesla is printed out with the shortcode [MARKE] on both type of pages:
https://moinmobility.de/marken/tesla/
https://moinmobility.de/marken/tesla/model-3/
I tried a lot of code chunks and always if the brand name was printed correctly on the parent page, the child one was wrong.
I have the following code:
function custom_meta_query( $meta_query ){
$rem_width = get_query_var('rem_width');
$rem_length = get_query_var('rem_length');
$meta_query[] = array( 'key'=>'remnant_width', 'value' => $rem_width, 'compare'=>'>=');
$meta_query[] = array( 'key'=>'remnant_length', 'value' => $rem_length, 'compare'=>'>=');
$meta_query['relation'] = 'AND';
return $meta_query;
}
// The main shop and archives meta query
add_filter( 'woocommerce_product_query_meta_query', 'custom_product_query_meta_query', 10, 2 );
function custom_product_query_meta_query( $meta_query, $query ) {
if( (! is_admin() ) && ((isset($_GET['rem_width'])) || (isset($_GET['rem_length']) ) ))
return custom_meta_query( $meta_query );
}
I want to order the products returned by width and currently it is showing alphabetical order.
'orderby' => 'meta_value_num',
'meta_key' => 'remnant_width',
'order' => 'ASC'
But I don't know how to incorporate it into what has gone before.
Thanks
i use a Toolset plugin to make view on WP and they give us possibility to manipulate with API.
I try to add argument to my view but it doesn't work.
here is the function :
add_filter( 'wpv_filter_query', 'add_city_tax', 99, 3 );
function add_city_tax( $query_args, $view_settings, $view_id )
{
if($view_id == 7706)
{
$args = array(
'tax_query' => array
(
array
(
[taxonomy] => 'ville',
[field] => 'id',
[terms] => Array
(
[0] => 220
),
[operator] => 'IN'
),
[relation] => 'OR',
),
);
}
$query_args = new WP_Query( $args );
return $query_args;
}
the page make an error
The method you are declaring args is not correct. You should not create array like that. Please check following example.
function add_city_tax( $query_args, $view_settings, $view_id ) {
if($view_id == 7706) {
$args = array(
'tax_query' => array(
array(
'taxonomy' => 'ville',
'field' => 'id',
'terms' => array( 220 ),
'operator' => 'IN',
),
'relation' => 'OR',
),
);
}
$query_args = new WP_Query( $args );
return $query_args;
}
And here doc
Description
When displaying a View listing posts, this filter is applied to the arguments being generated by the View settings before they are passed to the WP_Query class.
Views filters - wpv_filter_query
Note that the filters that you can add to the View are also hooked here, each of them using a different priority that gets up to 100. To ensure that your filter runs after them, you would need to use a higher priority number.
Remember that the filter can take three parameters. If you pass more than one, you need to specify it in your code using the fourth argument:
1
add_filter( 'wpv_filter_query', 'my_callback_function', 99, 3 );
Arguments
array $query_args Τhe query arguments as in WP_Query.
array $view_settings The View settings.
int $view_id The View ID.
No it doesn't work like this.
i have tried like this :
function add_city_tax( $query_args, $view_settings, $view_id ) {
if($view_id == 7706) {
$args = array(
'tax_query' => array(
array(
'taxonomy' => 'ville',
'field' => 'id',
'terms' => array( 220 ),
'operator' => 'IN',
),
'relation' => 'AND',
),
);
}
$query_args[] = $args ;
$msg = '<pre>' . print_r($query_args, true) . '</pre>';
mail('franck#efficonex.fr', 'test', $msg);
return $query_args;
}
add_filter( 'wpv_filter_query', 'add_city_tax', 99, 3 );
But it is the same result without filter.
here is the example of api
//Return only posts from the current author when listing posts of type company:
add_filter( 'wpv_filter_query', 'prefix_show_only_current_author' );
function prefix_show_only_current_author( $query_args ) {
global $current_user;
$types = (array) $query_args['post_type'];
if ( !is_admin() && in_array( 'company', $types ) ) {
$query_args['author'] = empty( $current_user->ID ) ? -1 : $current_user->ID;
}
return $query_args;
}
I have googled this issue for the last 4 days, and everything I have found has not fixed the issue.
My Custom Post Type's Custom Taxonomy template paging 404's unless I set the sites Blog posts per page to 1, which I cannot do, due to the fact that there will be a need to show more than 1 "blog post" per page...
How can I correct this issue?
LATEST FIX ATTEMPT
// fix the paging 404's on category/tag pages
add_action( 'pre_get_posts', function( $qry ) {
$_num_per_page = ( get_option( 'gyokb_option_name' )['gyokb_articles_pp'] ) ?? 5;
if( ( is_tax( 'kb_categories' ) || is_tax( 'kb_tags' ) ) && ! is_admin() ) {
$qry->set ( 'post_type', array( 'post', 'gyo_kb' ) );
$qry->set( 'posts_per_page', $_num_per_page );
}
} );
My CPT is here... (too much code to paste here...)
https://pastebin.com/SyKJwnCH
My Taxonomy is here...
https://pastebin.com/4BWWgGWq
And My Paging in the template:
<?php
echo paginate_links( array(
'format' => 'page/%#%',
'prev_text'=>' Previous ',
'next_text'=>' Next ',
'current' => max( 1, $page ),
'total' => $pg_ct,
'type' => 'plain',
'mid_size' => 4,
'paged' => $page, ) );
?>
with my WP_Query pulling it all together....
$args = array( 'post_type' => 'gyo_kb', 'posts_per_page' => $_num_per_page, 'paged' => $page,
'orderby' => 'date','order' => 'DESC',
'tax_query' => array(
array (
'taxonomy' => 'kb_categories',
'field' => 'slug',
'terms' => $cur_cat -> slug,
)
),
);
$the_query = new WP_Query( $args );
$page is populated with: $page = get_query_var( 'page' ); $page = ( ! empty( $page ) ) ? $page : 1;
All other variables are populating properly
I had the fix applying during the incorrect "boot" timing for wordpress. Initially I had placed in in the "wp" action.
I moved it to the "init" action, and now it works.
I am working on a project where I need to list all hidden products in a page. I have created a shortcode for that and I have used following meta query, but it's not working.
$meta_query = array(
'key' => '_visibility',
'value' => array('hidden'),
'compare' => 'IN'
);
Any idea why this is not working? Thank you in advance for your help.
$args = array(
'post_type' => 'product',
'meta_key' => '_visibility',
'meta_value' => 'hidden'
);
$query = new WP_Query($args);
Then your usual loop worked for me :)
WooCommerce 3+ visibility has been changed to a taxonomy instead of post meta and if you're using WC()->query->get_tax_query() function call for building the args, you can just remove that call from setting tax_query.
IF you don't have access to the args or how they are built and it is ran through get_tax_query you can use the filter to remove the NOT IN tax query for visibility:
add_filter( 'woocommerce_product_query_tax_query', 'smyles_show_all_products' );
// do something that calls some fn that makes product query
remove_filter( 'woocommerce_product_query_tax_query', 'smyles_show_all_products' );
function smyles_show_all_products( $tax_query ) {
foreach( (array) $tax_query as $t_index => $t_query ){
if( isset( $t_query['taxonomy'], $t_query['operator'] ) && $t_query['taxonomy'] === 'product_visibility' && $t_query['operator'] === 'NOT IN' ){
unset( $tax_query[ $t_index ] );
break;
}
}
return $tax_query;
}
Use this
'meta_query' => array(
array(
'key' => '_visibility',
'value' => array( 'catalog', 'visible' ),
'compare' => 'IN'
)
)