Wordpress theme options page query_posts - wordpress

I am busy building a small theme options page for one of clients and need some help with an issue.
currently i have the option to manually put in IDS of wordpress pages to extract the data with query_posts
based on the theme options is creates a variable called $euro_box_1_vehicles;
my options are filled in as 32,39,43,54 in the input, and when I print this statement with echo, I get the same result.
When I just replace array(32,39,43,45) with array($euro_box_1_vehicles) it only returns one result.
<?php
$vehicle1 = array(
'post__in' => array(32,39,43,45),
'post_type' => 'page',
);
query_posts( $vehicle1 );
while (have_posts()) : the_post();
?>

When I echo var_dump = string(11) "32,39,43,45"
In which case, you need to explode $vehicle1, since post__in expects an array;
query_posts(array(
'post_type' => 'page',
'post__in' => #explode(',', $vehicle1)
));

Update
When I just replace array(32,39,43,45) with array($euro_box_1_vehicles) it only returns one result.
Shouldn't you replace array(32,39,43,45) with $euro_box_1_vehicles not array($euro_box_1_vehicles)? The latter seems it would make a nested array with one argument, i.e. array(array(32,39,43,45)). Which is not what you want.
Old Answer....
If I read you right then query_posts() expects a list of IDs? (32,39,43,45)
But when you pass it $vehicle1 you are not giving it a list of IDs, but a 2-dimensional array.
<?php
$vehicle1 = array(
'post__in' => array(32,39,43,45),
'post_type' => 'page',
);
query_posts( $vehicle1['post_in'] ); //use sub-array that contains list
while (have_posts()) : the_post();
?>

Related

WP Query Get Posts by Comment Author ID and specific keyword?

I am trying to create a custom loop which gets the posts that author__in 1 has commented the specific keyword 'test' on and use that to build the recent posts list.
<?php $query_args = array('search' => 'test', 'author__in' => '1', 'post_status' => 'publish', ); $the_query = new WP_Comment_Query( $query_args );?> <?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
This query doesn't work, it gives me nothing, i guess ->have_posts does not exist for wp_comment_query?
How can i build a query using WP_query that lists the most recent posts with comment 'test' from author with id 1? Is it even possible?
Couple things here.
First, the "search" parameter is actually only "s", not the full word. Here's that reference: https://codex.wordpress.org/Class_Reference/WP_Query#Search_Parameter
Second, the author__in param takes an array, like this: $query = new WP_Query( array( 'author__in' => array( 2, 6 ) ) ); rather than a single integer or quoted digit. Here's that reference: https://codex.wordpress.org/Class_Reference/WP_Query#Author_Parameters
I think you could get what you want by just changing your code slightly. One thing to note, I'm not sure if the search parameter does a keyword search in comments though, it might only search post content. Try this:
$query_args = array('s' => 'test', 'author__in' => array(1), 'post_status' => 'publish', );
Let me know if this works for you.

WordPress trigger standard post list rendering

Is there a way to programmatically trigger the standard displaying post list from a custom WP_Query, just like a "category" menu item does?
To clarify:
i'm not looking for plugins like List category posts that do the work but with a custom built-in template..
I need the wp's(theme's) standard post list rendering loop to be triggered !
Thanks!
Put this code where ever you need to render list of post from category "example_category_slug", (or any other taxonomy).
$wpq = array ('taxonomy'=>'category','term'=>'example_category_slug');
$myquery = new WP_Query ($wpq);
$article_count = $myquery->post_count;
echo "<ul>";
if ($article_count){
while ($myquery->have_posts()) : $myquery->the_post();
echo "<li>".$post->post_title."</li>";
endwhile;
}
echo "</ul>";
or use simpler query, like $myquery = new WP_Query ('cat= 11, 9'); for cat ids, or any other wp_query
But while choosing where to put it, consider Template Hierarchy as a set of rules which template file is right to use.
If you need a list of categoires insted post use wp_list_categories
<?php
$args = array(
'show_option_all' => '',
'orderby' => 'name',
'order' => 'ASC',
'style' => 'list'
);
wp_list_categories($args);
?>
ful ref. in wp codex
Here is a way to do it..
inside a shortcode's function, or in a function that act ajax style, put a code like that:
global $wp_query;
$cat = $_GET['categoria'];
$wp_query->init();
$wp_query->query(array(
'posts_per_page' => 4,
'category_name' => $cat
));
get_template_part( 'blog', 'columns' );
wp_reset_query();
die();
the get_template_part arguments are theme dependent and can be desumed from php template names from theme's root directory.
in this case wp will call [theme's_dir]/blog-columns.php..
i reccomend the lecture #Michal reccomended me

Wordpress: retrieve first five items from all populated categories

I am trying to find a way to achive the following: I'd like to make a custom template. Within this template I'd like to list the name of each category on my site that has at least 1 item assigned to it. Beneath the category name (and link) I'd like to insert some content from the first x number of items that have that particular category assigned.
Just in case it makes a massive difference the items in question are not posts, but custom items.
Can anyone give me some pointers or help? I assume there will be something I can do with the wp_query function, but I'm not really sure how to inject it between each of the category titles, or indeed how to make it work with a category for which I can't explicitly provide an id in the code).
Thank you.
get_posts() is your ideal solution. For example:
<?php $posts_array = get_posts( $args ); ?>
<?php $args = array(
'posts_per_page' => 1,
'category' => $post_cat,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'yourcustompostname',
'post_status' => 'publish',
'suppress_filters' => true );
$myposts = get_posts( $args );
///respective loop here ie : foreach $myposts as $mypost ... etc
?>
Where $post_cat is the id of your category and post_type is the custom post type.
To keep these 'items' organized I would create a custom post and then assign a category to those posts, for each respective category (if there are multiple). If there is just one, than you could live with just using a single category.
EDIT:
to get the categories assigned to this custom post take a look at this link
Using the results from this you could assign the cat id's to the variable created earlier: $post_cat.
You have to use query_posts function
query_posts(array('category_name'=>'category-slug','posts_per_page'=>'5'));
then you need to do
while ( have_posts() ) : the_post();
and you can get the post id using:
$p_id = get_the_ID();
for more information : query_posts
You can use get_categories()
http://codex.wordpress.org/Function_Reference/get_categories
But, then you need to find out where the data for 'items' is stored, and query depending on that.
Here is the answer of your problem. You need to change the category id of own post category id .
Here is the code. just copy and past it.
$query1 = new WP_Query( array( 'post_type' => 'post','cat' =>'10,9') );
Use the above query in loop and after loop,reset your query . see the below code.
wp_reset_query();
Please use it and let me know if needs anything else.

Wordpress: alphabetice posts using custom category template

I am trying to display posts ordered alphabetically by title, only for a certain category. I have tried to follow the instructions in the Codex but I am confused because my code in the template pages looks quite different from the examples in the Codex.
I have a category named "designers" so I duplicated the category.php and named it category-designers.php. Inside, there is a call to a loop-designers.php
Inside the category-designers, I have tried the Codex pice of code:
$args = array( 'posts_per_page' => -1, 'orderby'=> 'title', 'order' => 'ASC' );
$glossaryposts = get_posts( $args );
foreach( $glossaryposts as $post ) : setup_postdata($post);
get_template_part('loop', 'designers');
endforeach;
But the output is weird: first it displays a list of posts ordered by date, then it displays the same posts but ordered alphabetically, only that the alphabetically ordered list is repeated as many times as posts are (9 in this case).
I know I must be doing something terribly wrong but can't find examples using the get_template_part, they all use a foreach just like in the Codex.
Thanks for your answers.
Edited: in my loop-designers.php I have basically the same as in the loop.php, but with modifications so for that category no date, tags or other info are shown. I pasted the HTML here http://jsfiddle.net/6qdvF/
With the piece of code you have there you are including a loop in a loop. Using get_posts() with a foreach loop is essentially a WP loop, then you are including get_template_part('loop', 'designers'); which is more than likely another loop.
You can remove get_template_part('loop', 'designers'); from your piece of code and just stick in your tags to get the desired content, (i.e the_content(); the_title; the_excerpt; etc..) or you can create a new loop with wp_query() like the below example.
<?php
$query = new WP_Query(array('post_type' => 'post', 'cat' => 1, 'posts_per_page' => -1, 'orderby' => 'title', 'order' => 'ASC'));
while ( $query->have_posts() ) : $query->the_post();
?>
// put your content template tags here
<?php endwhile; wp_reset_postdata(); ?>
This loop will query "Posts" and category 1. You can change 'post_type' => 'post', to query other post types by changing the word post to the post type name. Or change the category number to the desired category ID you wish to query.

how to retrieve WP_Query without ordering by date

I want to use
$ps = "9126,8955,8554,8620,8825,8912,8937,8813,9054,9022";
$recent = new WP_Query( array('post__in' => explode(',', $ps)) );
to retrieve posts by ID, I do and the result ordered by publish date but I want to retrieve in same of IDs in this example in this order:
9126,8955,8554,8620,8825,8912,8937,8813,9054,9022
Personally, if the order of posts as well as your ID numbers are going to be that strict, I would probably avoid using WP_Query:
<?php
$ids = array(9126,8955,8554,8620,8825,8912,8937,8813,9054,9022);
foreach($ids as $id)
{
$post = get_post($id);
setup_postdata($post);
?>
<!-- YOUR HTML HERE -->
<?php
}
?>
Otherwise, since Posts do not have any kind of Menu Order option like Pages do, you will probably have to set a Custom Field and give it a value (with lower numbers taking priority over higher numbers). Then you can do something like this:
<?php
$ids = array(9126,8955,8554,8620,8825,8912,8937,8813,9054,9022);
$recent = new WP_Query(array('post__in' => $ids, 'orderby' => 'meta_value_num', 'meta_key' => 'postOrder', 'order' => 'ASC'));
?>
This second option is untested and I can't guarantee it will work right off the bat, but it should get you started. Good luck.
in the future in wordpress 3.5 you can use: 'orderby'=>'post__in'

Resources