How to dynamically insert an orderby statement in a query array - php

I'm trying to dynamically assign the sort order (stored in $current_sort below) for a query that lists menu items.
If I hard code the sort order it works fine, however, when I try to dynamically assign the sort parameters to a string, it fails. What am I missing?
$current_sort = ", 'orderby' => 'title', 'order' => 'asc'";
$myposts = get_posts(
array(
'cat' => "$cat,-$catHidden",
'numberposts' => $my_current_count . $current_sort
));
//If I hard code the value of $current_sort it works fine
$myposts = get_posts(
array(
'cat' => "$cat,-$catHidden",
'numberposts' => $my_current_count,
'orderby' => 'title',
'order' => 'asc'));

You cannot turn a string into PHP source code. (At least you shouldn't.)
Try this:
$current_sort_order = "title";
$current_sort_direction = "asc";
$myposts = get_posts(array(
'cat' => "$cat,-$catHidden",
'numberposts' => $my_current_count,
'orderby' => $current_sort_order,
'order' => $current_sort_direction
) );

you are doing it wrong, you don't have to concate..try this:
$current_sort = "title";
$order = "asc";
$myposts = get_posts(
array(
'cat' => "$cat,-$catHidden",
'numberposts' => $my_current_count,
'orderby'=> $current_sort,
'order' => $order
));

The string concatenation on the line
'numberposts' => $my_current_count . $current_sort
is not equivalent to creating multiple array elements as in
'numberposts' => $my_current_count,
'orderby' => 'title',
'order' => 'asc'));
In the first instance, numberposts becomes a string containing information about the sort.
In the second instance, numberposts only contains the current count.
A better option may be:
$orderoption="<TITLE HERE>";
$order_dir="<SORT OPTION HERE>";
$myposts = get_posts(
array(
'cat' => "$cat,-$catHidden",
'numberposts' => $my_current_count,
'orderby' => $orderoption,
'order' => $order_dir));

Related

Combine Two PHP/MySQL queries

I'd like to combine two php/mysql queries against the wp_posts table using the code below.
However, as you can see, there's an error there with the second post_type. The second post_type is 'post', and is never recognized. :-(
How would I make that an "AND", to gather BOTH the 'membercontent' and 'post' data ? The same question applies to the 'value' statement just below it. 'true' and 'yes'.
<?php $args = array(
'post_type' => 'membercontent', 'post',
'meta_query' => array(
array(
'key' => 'tt_freemium',
'value' => 'true', 'yes',
),
),
'orderby' => 'post_date',
'order' => 'DESC',
'posts_per_page' => '200' );
`$ourposts = new WP_Query( $args );?>
Just figured it out. Duh. Had the answer all along....ARRAYS!
<?php $args = array(
'post_type' => array ('membercontent', 'post',),
'meta_query' => array(
array(
'key' => 'tt_freemium',
'value' => array ('true', 'yes',),
),
),
'orderby' => 'post_date',
'order' => 'DESC',
'posts_per_page' => '200' );
$ourposts = new WP_Query( $args );?>

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.

Wordpress Query Args - Only show post with meta_value greater than 0

I have a wordpress loop with an array of arguments to show only specific posts (any posts with a deposit_amount value of 0).
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => 'ASC',
'cat' => '11',
'meta_key' => 'deposit_amount',
'meta_value' => 0
);
$loop = new WP_Query( $args );
?>
I would like to create a similar array but showing posts with a deposit_amount meta_value of greater than 0
i have tried to use the php greater than operator but breaks the code.
'meta_value' => >0
Can anyone point me in the right direction with this problem?
just found 'meta_compare' => '>'
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => 'ASC',
'cat' => '11',
'meta_key' => 'deposit_amount',
'meta_value' => 0,
'meta_compare' => '>'
);
$loop = new WP_Query( $args );
?>
Use a Meta Query:
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => 'ASC',
'cat' => '11',
'meta_query' => array(
array(
'key' => 'deposit_amount',
'value' => 0,
'compare' => '>'
)
)
);

How Can I Do LIMIT 1, 2 In WP_Query

I am using Wp_Query method and I want to learn is there any parameter to limit my query.
I try to use LIMIT 1,2 in WP_Query.
Thanks for advance!
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'orderby' => 'ID',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'featured',
'value' => 'evet',
'compare' => '='
)
)
);
// The Query
$the_query = new WP_Query( $args );
you can use posts_per_page and offset args for WP_Query() like:-
$query = new WP_Query( array( 'posts_per_page' => 5, 'offset' => 3 ) );
for more :- http://codex.wordpress.org/Class_Reference/WP_Query#Pagination_Parameters
you are asking for LIMIT 1,2 (means offset,count(how many rows)) so
posts_per_page = count
offset = offset

Get posts multiple categories

I want to get more than one category with wordpress get posts function.
This is what I have:
$posts = get_posts(array(
'posts_per_page' => -1,
'category_name' => 'featured'
)
);
And I want to add the category "artworks". I tried like this but it doesn't work, any ideas how to do it?
$posts = get_posts(array(
'posts_per_page' => -1,
'category_name' => 'featured', 'artworks'
)
);
As noted in the Wordpress docs it looks like you have to pass category as a number, and I don't see 'category_name' as an option in the documentation.
It also appears that you can pass only a single category per get_posts() call.
So for a single category:
$posts = get_posts(array(
'posts_per_page' => -1,
'category' => '2'
)
);
Or for multiple
$vars = array(
array(
'posts_per_page' => -1,
'category' => '2'
),
array(
'posts_per_page' => -1,
'category' => '3'
)
);
foreach $vars as $post_array{
$posts[] = get_posts($post_array);
}
And print_r($posts); will have the resulting array.

Categories