WordPress trigger standard post list rendering - wordpress

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

Related

wordpress display all used categories per custom post type

Im trying to display all used categories per custom post type, for example on the projects page, I show all projects, and at the top I want to add loop all used categories for projects. When I use wp_list_categories() it wil show ALL categories, even the onces that are not related to the cpt projects.
Try something like this :
$categories = get_categories( array(
'orderby' => 'name',
'parent' => 0,
'exclude' => 1
) );
Loop through
<?php foreach ( $categories as $cat ) { ?>
<?php echo $cat->term_id; ?>
<?php echo $cat->name; ?>
<?php } ?>
And then
query_posts('post_type=post&post_status=publish&posts_per_page=3&cat=-1&paged='. get_query_var('paged'));
You should save all the categories when you loop through your posts, in an array or other data structure you find useful.
Using an associative array would be a simple solution. With category as key and category archive URL as value, you would have all the components needed to create a link to each category.
['category 1' => 'http://yoururl.com/category1',...]
Example
foreach ($posts as $post) {
$category = get_the_category($post->ID);
...
// using $category, get the title and URL for it
}
From here you can get all the properties you need for each category.

Accessing Advanced Custom Fields by Page Name

I am trying to retrieve all advanced custom fields tied to a particular page. This is different than iterating through posts, I am familiar with the following:
$posts = get_posts(array(
'post_type' => 'post_name',
'meta_key' => 'color',
'meta_value' => 'red'
));
However this method is specific to posts and does not allow me to retrieve all ACF by page name.
I appreciate any suggestions on how to accomplish this.
The are too ways to do this that come to mind...
1. Using the Loop
Using WP_Query you can do something like this...
<?php
// WP_Query arguments
$args = array (
'pagename' => 'homepage',
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
the_field( "field_name" );
}
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();
?>
In place of the 'homepage' in 'pagename' => 'homepage', you want to put the page slug of your page. And of course in place of the_field( "text_field" ); you want to add your fields/content.
You can also query by Page ID and some other parameters. You can find all other parameters that you can use here:
https://codex.wordpress.org/Class_Reference/WP_Query#Post_.26_Page_Parameters
2. Adding second parameter to the_field() function
More simple way, without custom loop, is just to use ACF's built-in the_field() function with the $post->ID parameter added as a second parameter.
<?php the_field('field_name', 123);
This might be the way to go since you want to show the content of only one page, and therefore don't really need to loop.
Reference: http://www.advancedcustomfields.com/resources/how-to-get-values-from-another-post/
You can use ACF's get_fields() function -
<?php $fields = get_fields( $post->ID ); ?>
You could then loop through them or simply print the array for testing.
http://www.advancedcustomfields.com/resources/get_fields/

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.

Is it possible to request articles that match more than one category in WordPress API?

These apis work perfectly.
http://domain.com/wp/api/get_tag_posts/?tag_slug=banana
http://domain.com/wp/api/get_category_posts/?category_slug=featured
Is there any way to join multiple tags or categories into a single request?
E.g.
http://domain.com/wp/api/get_category_posts/?category_slug=featured,news
or
http://domain.com/wp/api/get_category_posts/?category_slug=featured|news
I'm hoping to do what in SQL would be a "where category_name in ('featured','news')
You are after this
<?php $args = array(
'posts_per_page' => 10,
'category__not_in' => array(5,151)
);
query_posts($args);?>
This is what you need to do in PHP to obtain multiple posts from multiple categories. Now in case you are also looking for json or xml or any format it spits out. Put a new function in functions.php and register it with
add_action( 'wp_ajax_nopriv_getmyjson', 'myfunctionname' );
add_action( 'wp_ajax_getmyjson', 'myfunctionname' );
function myfunctionname()
{
Global $wpdb;
...
}
Call this in your theme or plugin and use action=getmyjson and url goes to admin_ajax with nonce set. After Global $wpdb you can use above function to bring all posts and then through them out as json object. Something like this
$response = json_encode( array(
'success' => true,
'message' => $ajaxmsg
'posts' => $mypostarray
)
);
// response output
header( "Content-Type: application/json" );
echo $response;
die(); //This would then make sure it is not getting anything else out of wordpress and sends only json out.
Once this is all done. You will have multiple posts out put in json format.
I needed to do the same thing and ended up using this plugin
Query Multiple Taxonomies. This plugin lets you do faceted search using multiple custom taxonomies. (author's description).
http://wordpress.org/extend/plugins/query-multiple-taxonomies/
It's got a handy sidebar widget also. Hope this helps!
Have you tried making the category_name an array? e.g.
<?php $args = array(
'posts_per_page' => 10,
'category_name' => array('featured', 'news')
);
query_posts($args);?>
Also, with the tag, I found this example:
<?php query_posts('cat=32&tag=hs1+hs1&showposts=10'); ?>
or
<?php query_posts(array(
'posts_per_page' => 10,
'category_name' => array('featured', 'news'),
'tag_slug__and'=>array('banana')
)); ?>

Wordpress theme options page query_posts

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();
?>

Resources