I can't set up an infinite scroll system.
I created a table, on each line there is a product (music with duration, buy button etc).
I want to have 10 or 15 songs when loading, and display 10 or 15 more when scrolling.
Here is my basic code:
if ( ! empty( $player_shortcode_atts['category'][0] ) ) {
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
'fields' => 'ids',
'post_type' => 'download',
'no_found_rows' => true,
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'download_category',
'field' => 'name',
'terms' => $player_shortcode_atts['category'],
),
),
);
} else {
$args = array(
'fields' => 'ids',
'post_type' => 'download',
'no_found_rows' => true,
'posts_per_page' => -1,
);
I searched on google, forum etc and I couldn't find a solution.
Related
I'm using get_terms to show a list of terms. It works fine so far.
But I want to hide every term with out of stock products.
If I use 'hide_empty' => true it wouldn't work because the products are there and published.
Is there a way to add the _stock meta field to the get_terms function?
Unfortunately I have to use get_terms.
Here's my code (it's a lot bigger but that's the part):
$terms = get_terms( array(
'taxonomy' => 'product_tax',
'orderby' => 'name',
'hide_empty' => true,
) );
5 months ago but maybe it will help someone : I'm facing the same issue and all I found is to make a foreach to unset the empties values.
foreach ($terms as $key => $term) {
$args = array(
'post_type' => 'product',
'paged' => 1,
'posts_per_page' => 1,
'order' => 'DESC',
'post_status' => 'publish',
'orderby' => 'publish_date',
'meta_query' => array( array(
'key' => '_stock_status',
'value' => 'instock',
) ),
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $term->term_id
)
)
);
$loop = new WP_Query( $args );
if($loop->post_count < 1) {
unset($terms[$key]);
}
}
I want to get my most viewed posts, and I'm using yuzo plugin for count views.. But this code doesn't work.. How can I make it work ?
$popular = new WP_Query( array(
'post_type' => array( 'post' ),
'showposts' => $instance['popular_num'],
'cat' => $instance['popular_cat_id'],
'ignore_sticky_posts' => true,
'orderby' => 'wpng_yuzoviews.views',
'order' => 'dsc',
'date_query' => array(
array(
'after' => $instance['popular_time'],
),
),
) );
Your query will be something like:
$query = new WP_Query( array(
'meta_key' => 'post_views_count',
'orderby' => 'meta_value_num',
'posts_per_page' => 5
) );
By default, the ordering will be highest to lowest, thus giving you the "top" 5.
I have 2 custom post types created via theme, rt_book and rt_chapter. Each rt_book has several rt_chapter posts. When I use single-rt_book.php (which should show the contents of a specific rt_book post), how can I filter only the rt_chapter belongs to that specific rt_book being loaded?
My current WP_Query is as follow (which returns all rt_chapter):
$args = array(
'post_type' => 'rt_chapter', // TODO: Filter specific book.
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts' => 1,
'orderby' => 'date',
'order' => 'ASC'
);
$query = new WP_Query($args);
if($query->have_posts()) {
while($query->have_posts()) {
$query->the_post();
$posts[] = $query->post;
}
}
I relate these two custom post types by setting the Post Meta of rt_book with an array of rt_chapter post IDs, via function add_post_meta({post ID of rt_book}, 'chapter_orders', {post ID of rt_chapter}) and update_post_meta().
Tried child_of option of WP_Query, but does not affect the results.
Try Like this
$args = array(
'post_type' => 'rt_chapter',
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts' => 1,
'orderby' => 'date',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'rt_book',
'value' => get_post_meta(get_the_ID(), 'chapter_orders', true),
'compare'=> 'IN'
),
)
);
$query = new WP_Query($args);
if($query->have_posts()) {
while($query->have_posts()) {
$query->the_post();
$posts[] = $query->post;
}
}
Try this. Hopefully this should work.
$tax_slug = get_query_var('taxonomy');
$args = array(
'post_type' => 'rt_chapter', // TODO: Filter specific book.
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts' => 1,
'orderby' => 'date',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'rt_book',
'field' => 'slug',
'terms' => $tax_slug
),
)
);
I'm having a problem getting my query function. I need to run the loop, excluding a particular category.
I'm trying to use category__not_in, but is not working at all some.
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'category__not_in' => array( '44' ),
'posts_per_page' => 9,
'paged' => get_query_var('paged')
);
$query = new WP_Query( $args );
query_posts($query);
?>
I've already tried:
'category__not_in' => array( '44' ),
'category__not_in' => array( 44 ),
'category__not_in' => '44',
'category__not_in' => 44,
But nothing works =(
Try using tax_query instead :
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 9,
'paged' => get_query_var('paged'),
'tax_query' => array(
array(
'taxonomy' => '<YOUR TAXONOMY NAME>',
'field' => 'term_id',
'terms' => array( 44 ),
'operator' => 'NOT IN',
),
),
);
$query = new WP_Query( $args );
query_posts($query);
?>
Use 'cat' => '-44' in your $args array:
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'cat' => '-44',
'posts_per_page' => 9,
'paged' => get_query_var('paged')
);
It's the way recommended in the WP Codex.
Thanks guys, it worked thanks to #rnevius
The problem was in my query, I was using WP_Query() and query_posts().
I used how reference the WP Codex: https://codex.wordpress.org/Class_Reference/WP_Query
Below is how my code was at the end:
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'category__not_in' => array( 44 ),
'posts_per_page' => 9,
'paged' => get_query_var('paged')
);
$query = new WP_Query( $args );
?>
<?php
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
?>
// code
<?php
}
} else {
// no posts found
}
wp_reset_postdata();
?>
To exclude a category in the search use this:
function search_filter($query)
{
if ( !is_admin() && $query->is_main_query() ) {
if ($query->is_search)
{
$taxquery = array(
array(
'taxonomy' => 'category',
'field' => 'term_taxonomy_id',
'terms' => 244,
'operator' => 'NOT IN',
)
);
$query->set( 'tax_query', $taxquery );
}
}
}
add_action('pre_get_posts','search_filter');
Good morning, I found many similar questions, but none of the answer fit to my problem. The point is very simple: I have a custom loop with get_posts(), and I want to exclude current post from being displayed.
The code is:
$args = array(
'posts_per_page' => 3,
'orderby' => 'meta_value',
'order' => 'ASC',
'post_type' => 'fasthomepress_pt',
'post__not_in' => array(get_the_id()),
'meta_query' => array(
array(
'key' => 'custom_richiesta',
'value' => array($custom_boxes['custom_richiesta'][0] - 10000, $custom_boxes['custom_richiesta'][0] + 10000 ),
'type' => 'numeric',
'compare' => 'BETWEEN'
)
)
);
I tried with:
'post__not_in' => array(get_the_ID),
'post__not_in' => array($post->ID),
'exclude' => $post->ID,
'exclude' => get_the_ID,
and with many other combinations with or without array. Of curse, current post id is correctly echoed before this loop, and if I try echo($post->ID) and echo(get_the_ID()) I have the same, correct, result.
I really don't know what's happening,
thank you very much for help,
Marco
Try exclude.
$args = array(
'posts_per_page' => 3,
'orderby' => 'meta_value',
'order' => 'ASC',
'post_type' => 'fasthomepress_pt',
'exclude' => array(get_the_id()),
'meta_query' => array(
array(
'key' => 'custom_richiesta',
'value' => array($custom_boxes['custom_richiesta'][0] - 10000, $custom_boxes['custom_richiesta'][0] + 10000 ),
'type' => 'numeric',
'compare' => 'BETWEEN'
)
)
);
here is a function that does just that:
function get_lastest_post_of_category($cat){
$args = array( 'posts_per_page' => 1, 'order'=> 'DESC', 'orderby' => 'date', 'category__in' => (array)$cat);
$post_is = get_posts( $args );
return $post_is[0]->ID;
}
Usage: say my category id is 22 then:
$last_post_ID = get_lastest_post_of_category(22);
you can also pass an array of categories to this function.
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'posts_per_page' => 18,
'paged' => $paged,
'offset' => 0,
'post__not_in' => array($last_post_ID,),
'category' => '',
'category_name' => '',
'orderby' => 'post_date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'post_status' => 'publish',
'suppress_filters' => true
);
// The Query
$the_query = new WP_Query( $args );