WordPress issue with stick posts - php

I'm using sticky posts to allow certain posts to be pinned in a featured post area. I have it working on a development server but when I moved it to the live server it only partially works. If there is a sticky post it displays it. But if there isn't a sticky post it displays nothing and it should display the most recent post. Is there an alternate way to handle this that may work?
$options = array(
'post_type' => post,
'posts_per_page' => 1,
'post__in' => get_option( 'sticky_posts' ),
'ignore_sticky_posts' => 1,
'status' => 'publish'
);

You can check result of get_option( 'sticky_posts' ) first:
$sticky = get_option('sticky_posts');
if ( !empty($sticky) ) {
// query options for sticky posts
$options = array(
'post_type' => post,
'posts_per_page' => 1,
'post__in' => $sticky,
'ignore_sticky_posts' => 1,
'status' => 'publish'
);
} else {
// query options for the most recent post
$options = array(
'posts_per_page' => 1,
'paged' => 1,
'orderby' => 'post_date',
'order' => 'DESC',
'post_status' => 'publish'
);
}

Related

How to get most viewed posts using another table in Wordpress

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.

WP_Query to filter specific parent Custom Post Type?

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
),
)
);

WP_Query twice on one page - not getting the desired posts per page

I have a custom post type which has an ACF check box to define if an post is featured or not. I wanted to show 6 featured and 6 non featured posts on a page and so created 2 WP_Query loops both with separate args. Whilst the separation of featured and non-featured is working, the page only shows 6 posts in total and I'm not sure how to resolve that?
My code:
<?php
$args1 = array(
post_type => 'fairs',
posts_per_page => -1,
showposts => 6 ,
meta_key => 'start',
orderby => 'meta_value_num',
order => 'ASC'
);
$new1 = new WP_Query($args1);?>
<?php while ($new1->have_posts()) : $new1->the_post();?>
<?php $field = get_field( 'wa_feature', get_the_ID() ); if ( true == $field ):?>
Featured Posts
<?php endif;?>
<?php endwhile; wp_reset_postdata();?>
<?php
$args2 = array(
post_type => 'fairs',
posts_per_page => -1,
showposts => 6 ,
meta_key => 'start',
orderby => 'meta_value_num',
order => 'ASC'
);
$new2 = new WP_Query($args2);?>
<?php while ($new2->have_posts()) : $new2->the_post();?>
<?php $field = get_field( 'wa_feature', get_the_ID() ); if ( false == $field ):?>
Non-featured Posts
<?php endif;?>
<?php endwhile;?>
Your WP_Query isn't right. You do two completely identical queries ($args1 = $args2), and expects diffrent results from them. Also you put meta_key into query arguments, but without telling what and how to compare.
Depending on what type you have chosen, the right syntax, if your meta filed is named featured and the query can be:
args1 = array(
'post_type' => 'fairs',
'posts_per_page' => -1,
'meta_key' => 'featured',
'meta_value' => true //or 1, or 'yes` depending of ACF type you have choose
'orderby' => 'meta_value_num',
'order' => 'ASC'
);
args1 = array(
'post_type' => 'fairs',
'posts_per_page' => -1,
'meta_key' => 'featured',
'meta_value' => false//or 0, or 'no`
'orderby' => 'meta_value_num',
'order' => 'ASC'
);
or if this featured field exists only in featured posts then
args1 = array(
'post_type' => 'fairs',
'posts_per_page' => -1,
'orderby' => 'meta_value_num',
'order' => 'ASC'.
'meta_query' => array(
array(
'key' => 'featured',
'compare' => 'EXISTS',
),
)
);
args1 = array(
'post_type' => 'fairs',
'posts_per_page' => -1,
'orderby' => 'meta_value_num',
'order' => 'ASC'.
'meta_query' => array(
array(
'key' => 'featured',
'compare' => 'NOT EXISTS',
),
)
);
Check ACF query or Codex for the right sintax.
Your arguments should be quoted:
array(
'post_type' => 'fairs',
'posts_per_page' => 6,
'meta_key' => 'start',
'orderby' => 'meta_value_num',
'order' => 'ASC'
);
Array keys can either be integers, or strings. Read more in the PHP array() docs.
You should also only use posts_per_page in this case (and remove showposts, which was replaced by posts_per_page in WP 2.1).

Output blog posts only onto a part of webpage

By default, blog posts can be shown on single page.
i want to customize a front page and display the posts only on the part of my page.
If it's possible, how can i output my latest blogposts onto a custom div or element?
Create custom loop, something like this:
http://www.johnmorrisonline.com/how-to-create-a-custom-loop-in-wordpress-using-wp_query/
So, basically use that WP_Query to get list of post and loop trough it:
http://codex.wordpress.org/Class_Reference/WP_Query
Here is an example how I do it:
$args = array(
'posts_per_page' => 3,
'offset' => 0,
'category' => 6,
'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 );
$posts_array = get_posts( $args );
// print_r($posts_array);
foreach($posts_array as $current_post){
print_r($current_post); // here you have current post values
}
http://codex.wordpress.org/Template_Tags/get_posts

Wordpress query for preview?

I am looking for an equivalent of getting a page/post with WP Query or get_posts e.g.:
$args=array(
'category' => 1,
'name' => 'my-page',
'post_type' => 'page',
'post_status' => 'publish',
'numberposts' => 1
);
$my_post = get_posts($args);
but I want this for a preview, only thing I have are the preview GET parameters:
?preview=true&preview_id=5&preview_nonce=b0d41a7fdb
The preview_id is actually the ID of the created post, not the id of the preview.
Any ideas?
So I eventually found a possible answer - this should retrieve the latest preview or "revision":
$args = array(
'post_status' => 'any',
'post_parent' => intval($_GET['preview_id']),
'post_type' => 'revision',
'sort_column' => 'ID',
'sort_order' => 'desc',
'posts_per_page' => 1
);
$my_post = get_posts($args);

Categories