Wordpress 'pre_get_posts' not working on ajax? - wordpress

Im using a plugin to select featured post which uses 'pre_get_posts' to filter posts.It works fine in normal query_post() but it is not working inside ajax. Here is my code
<?php
add_action('wp_ajax_portscroll', 'portscroll');
add_action('wp_ajax_nopriv_portscroll', 'portscroll');
function portscroll(){
?>
<?php
$offset_click= $_POST['data'];
$offset= 4+$offset_click*2;
$args = array(
'post_type' => 'portfolio',
'posts_per_page' => 2,
'featured' => 'yes',
'orderby' => 'menu_order',
'order' => 'ASC',
'offset'=> $offset
);
$post_osrtfolios = query_posts($args);
if ($post_osrtfolios) :
foreach ($post_osrtfolios as $post_osrtfolio) :
///contents goes here
endforeach;
endif;
wp_reset_query();
} ?>
Everything works fine, query works fine but the parameter 'featured' => 'yes' is not working. This parameter is from plugin.

$args = array(
'post_type' => 'portfolio',
'posts_per_page' => 2,
'meta_query' => array(
array(
'key' => '_is_featured',
'value' => 'yes'
)
),
'orderby' => 'menu_order',
'order' => 'ASC',
'offset'=> $offset
);
Use this

Related

Prioritizing wp_query by meta key

I have two custom fields for views. weekly_views and all_views. The weekly views custom field is deleted every week and starts counting views again from 0. So now what I want to achieve is show 12 posts by weekly views but when the custom field is deleted and unless there are views on those posts the query shows nothing. I want to show here posts by all_views instead of no posts.
My query goes as follows but it's not working as I want. In short what I want to achieve is to show posts by weekly_views custom field but if there's no post then show posts by all_views. And also if there's less than 12 posts by weekly_views then show weekly_views posts first and then remaining posts by all_views.
$args = array(
'post_type' => array( 'custom_post_type_1', 'custom_post_type_2'),
'posts_per_page' => '12',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'weekly_views',
),
array(
'key' => 'all_views',
),
),
);
The above code is returning me posts but are sorted by all_views.
Edit
The new query that's working for me
<?php
$args = array(
'post_type'=> array( 'custom_post_type1', 'custom_post_type2'),
'posts_per_page' => '12',
'meta_key' => 'weekly_views',
'orderby' => 'meta_value_num',
'order' => 'DESC',
);
$the_query = new WP_Query( $args );
if ($the_query->post_count < 12) {
$countweeklyposts = $the_query->post_count;
$showallpostscount = 12 - $countweeklyposts;
$args2 = array(
'post_type'=> array( 'band', 'artist'),
'posts_per_page' => $showallpostscount,
'meta_key' => 'all_views',
'orderby' => 'meta_value_num',
'order' => 'DESC',
);
$the_query2 = new WP_Query( $args2 );
}
?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
//Code to show posts goes here
<?php
endwhile;
wp_reset_postdata();
?>
<?php while ($the_query2 -> have_posts()) : $the_query2 -> the_post(); ?>
//Code to show posts goes here
<?php
endwhile;
wp_reset_postdata();
?>
You could do this too if you want a little less code
<?php
$args = array(
'post_type'=> array( 'custom_post_type1', 'custom_post_type2'),
'posts_per_page' => '12',
'meta_key' => 'weekly_views',
'orderby' => 'meta_value_num',
'order' => 'DESC',
);
$args2 = array(
'post_type'=> array( 'band', 'artist'),
'posts_per_page' => '12',
'meta_key' => 'all_views',
'orderby' => 'meta_value_num',
'order' => 'DESC',
);
if ($query->post_count > 12) {
$query_args = $args;
}else if($query->post_count < 12){
$query_args = $args2;
}
$query = new WP_Query( $query_args );
while ($query -> have_posts()) : $query -> the_post();
//Code to show posts goes here
endwhile;
wp_reset_postdata();
?>

Wordpress get Custom Post by Custom Taxonomy

I created a custom post and Custom Taxonomy
I need to search post using taxonomy
used this code but result is null
$properties = new WP_Query(
array( 'posts_per_page' => 10,
'orderby' => 'ID',
'order' => 'DESC',
'post_type' => 'real-property',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'tenure',
'terms' => 'val',
'field' => 'slug'
)
),
)
);
I found a solution myself: issue is WP_Query() function passed valued in array
<?php
$args = array( 'posts_per_page' => 10,
'orderby' => 'ID',
'order' => 'DESC',
'post_type' => 'real-property',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'tenure',
'terms' => 'val',
'field' => 'slug'
)
),
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
the_title();
endwhile;
wp_reset_postdata();
else :
esc_html_e( 'Sorry, no posts matched.' );
endif;
?>

How to query specific custom post with custom fields in a post_type?

I'm new into wordpress and kinda confused how to get 1 post in a custom post. I only knew the loop where it echo all the content in a post_type.
I'm aiming to get 1 post from a post_type 'product-category' and the meta_key is 'product-category-and-type'
You could try this query for custom in wordpress:
$args = array(
'post_type' => 'product-category',
'post_status' => 'publish',
"numberposts" => 1,
'meta_query' => array(
array(
'key' => 'product-category-and-type',
'value' => 'meta_value'
)
)
);
$getPosts = new WP_Query($args);
Just copy and paste below mentioned code at your desired position and replace "your_meta_value" with your actual meta value for meta key "product-category-and-type".
You will get your expected result :
<?php $args = array(
'post_type' => 'product-category',
"numberposts" => 1,
'post_status' => 'publish',
'meta_query' => array(array('key' => 'product-category-and-type','value' => 'your_meta_value'))
);
$myposts = new WP_Query($args);
while($myposts->have_posts()) : $myposts->the_post();
the_title();
endwhile;?>
$args = array(
'post_type' => 'product-category',
'post_status' => 'publish',
'posts_per_page' => '1',
'tax_query' => array(
array(
'taxonomy' => 'product-category-and-type',
'field' => 'slug'
),
),
);
$result = new WP_Query($args);

How do I show most viewed posts on a per-month basis?

For last month this query worked, now this 3 days (new month started), no posts at all:
<?php $current_year = date('Y'); ?>
<?php $current_month = date('m'); ?>
<?php endif;
arras_featured_loop( arras_get_option('featured1_display'),
apply_filters('arras_featured1_query', array(
'list' => $featured1_cat,
'taxonomy' => arras_get_option('featured1_tax'),
'query' => array( 'posts_per_page' => 15,
'meta_key' => 'post_views_count',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'year' => $current_year,
'monthnum' => $current_month,
'category_name' => Music,
'posts_per_page' => $featured1_count,
'exclude' => $post_blacklist,
'post_type' => arras_get_option('featured1_posttype')
)
) ) );
?>
Any kind of help will be appreciated.
UPDATE:
This was not pissible.
But now I found way to do this with plugin Wordpres Popular Posts.
Its working alone in template with this code:
<?php
if (function_exists( 'wpp_get_mostpopular' )) {
wpp_get_mostpopular('range=weekly&cat=276&order_by=views&limit=8');
}
?>
Now, what I need to do is to use this code up into this template down:
<?php endif;
arras_featured_loop( arras_get_option('featured1_display'), apply_filters('arras_featured1_query', array(
'list' => $featured1_cat,
'taxonomy' => arras_get_option('featured1_tax'),
'query' => array(
'posts_per_page' => $featured1_count,
'exclude' => $post_blacklist,
'post_type' => arras_get_option('featured1_posttype')
)
) ) );
?>
I think I need to put it inside here:
'query' => array(
Problem is I don't know to turn query from existing to one with arrows 'range=weekly' to 'range' => weekly didnt worked.

WordPress Loop - skip posts without a thumbnail

I want skip every post that has no thumbnail. The code does not work properly yet.
Actually the script doesn't show posts without a thumbnail - that's good, but in the loop the post with no thumbnail is still counted as a post.
So when i have for example 10 posts in my wordpress database. I want show 5 of them. But only the posts who has a thumbnail.
<ul>
<?php
$args = array( 'numberposts' => 5,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish'
);
$my_posts = get_posts( $args );
global $post;
foreach( $my_posts as $post ) : setup_postdata($post);
if ( !has_post_thumbnail() ) {
continue;
} else {
?>
<li>
<div class="clearfix" >
<div class="thumb"><?php the_post_thumbnail('post-image-big'); ?></div>
<?php the_title(); ?>
<p class="category"><?php the_category(', '); ?></p>
</div>
</li>
<?php } ?>
<?php endforeach; ?>
</ul>
Try
$args = array( 'numberposts' => 5,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish' ,
'meta_query' => array(
array(
'key' => '_thumbnail_id',
'compare' => '!=',
'value' => ''
)
)
);
or this if checking for an empty string didn't work for you
$args = array( 'numberposts' => 5,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish' ,
'meta_query' => array(
array(
'key' => '_thumbnail_id',
'compare' => '!=',
'value' => null
)
)
);

Resources