I'm attempting to avoid querying all of my site's posts, as there are many and the site becomes slow when this script runs.
I've read every similar question and done the following without success:
using showposts parameter
adding wp_reset_query();
passing args as an array
but nothing seems to work, it always returns all the posts.
$the_query = new WP_Query( 'posts_per_page=8' );
$blogs = [];
while ($the_query -> have_posts()): $the_query -> the_post();
$post_categories = [];
foreach (get_the_category($post->ID) as $key => $cat) {
if($cat->term_id == 2) continue;
array_push($post_categories,
"<a href='". get_category_link($cat->term_id) ."'>{$cat->name}</a>"
);
}
$blogs[] = [
'title' => get_the_title($post->ID),
'abstract' => substr(wp_strip_all_tags($post->post_content), 0, 80)
];
endwhile;
}
Related
I used this code to my category.php and I want it to convert for my page template, this code is fully functional and working on category.php.
<?php
if (in_category('interior')) {
$cat = get_query_var('cat');
$this_category = get_category($cat);
$this_category = wp_list_categories('hide_empty=0&hierarchical=false&order=ASC&orderby=title&show_count=0&title_li=&use_desc_for_title=1&child_of='.$this_category->cat_ID."&echo=0");
if($this_category !='<li>No categories</li>') {
echo '<ul class="ul-menu">'.$this_category.'</ul>';
}
}
?>
Image Attached Sample
Use this code into your page template.
$args = array (
'cat' => array(2,6,9,13),//use category id
'posts_per_page' => -1, //showposts is deprecated
'orderby' => 'date' //You can specify more filters to get the data
);
$cat_posts = new WP_query($args);
if ($cat_posts->have_posts()) : while ($cat_posts->have_posts()) : $cat_posts->the_post();
get_template_part( 'content', 'page' );
endwhile; endif;
I think its work for you. check and let me know
Can anyone explain why this only returns the 1st result only. I want to return all results that have the same custom field value as the current url. It will ony return 1. Does it need a for each or something? Thank you!!
<?php add_shortcode( 'feed', 'display_custom_post_type' );
function display_custom_post_type(){
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
$args = array(
'post_type' => 'custom_post_type',
'posts_per_page' => -1
);
$new_query = new WP_Query($args);
while($new_query->have_posts()) : $new_query->the_post();
return get_title();
endwhile;
};?>
Because you have added the return inside the loop it will only display the last one. You have to place the return outside the loop for it to display all.
Just a small example to see it work, change this bit:
while($new_query->have_posts()) : $new_query->the_post();
return get_title();
endwhile;
to this:
while($new_query->have_posts()) : $new_query->the_post();
$all_titles .= get_title();
endwhile;
return $all_titles;
It will most probably show all the titles in a single row, so just format as you wish!
You "return" from the function after the first element inside the while loop.
example returning all the posts:
$args = array(
'post_type' => 'custom_post_type',
'posts_per_page' => -1
);
$results = get_posts($args);
return $results;
Currently this is working code that is queried as standalone search.php.
I have an issue that it searches beginning of title and not anywhere in title what I exactly need.
I have a feeling that I'm close to solution and I tried couple of things and it is probably something that I'm missing in post $args..
global $wp_query;
$type = 'qa';
$args=array(
'post_type' => $type,
'post_status' => 'publish',
'showposts'=>100,
'orderby'=> 'menu_order',
'order' => 'DESC'
);
$my_query = null;
$my_query = new WP_Query($args);
$data = array();
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post();
$title = get_the_title();
$url = get_the_permalink();
$data[$title] = $url;
endwhile;
}
if(isset($_POST['latestQuery'])){
$latestQuery = $_POST['latestQuery'];
$latestQueryLength = strlen($latestQuery);
$result = array();
foreach($data as $name => $url){
if (substr(strtolower($name),0,$latestQueryLength) == strtolower($latestQuery)){
$result[$name] = $url;
}
}
echo json_encode($result);
}
This is already working in local environment.
JavaScript queries search.php file (above) where it gets json array returned based on query... for example "cla.."
{"Classic something":"http:\/\/www.something\/qa2","Classic something else":"http:\/\/www.something\/qa"}
So I have properly retuned two items that only have "Classic" mentioned in post title.
If I type "som" or "els" it doesn't return anything and it should match as well all "some" and "else" items.
I think it is not an issue with javascript code but with PHP code.
Thanks!
Solved if would be helpful to someone in future.
if(isset($_POST['latestQuery'])){
$latestQuery = $_POST['latestQuery'];
global $wp_query;
$type = 'qa';
$args=array(
'post_type' => $type,
'post_status' => 'publish',
's' => "$latestQuery", // defined query
'showposts'=>100,
'orderby'=> 'menu_order',
'order' => 'DESC',
);
$my_query = null;
$my_query = new WP_Query($args);
$data = array();
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post();
$title = get_the_title();
$url = get_the_permalink();
$data[$title] = $url;
endwhile;
}
$result = array();
foreach($data as $name => $url){ // removed if
$result[$name] = $url;
}
echo json_encode($result);
}
I was hoping someone could help me with this problem.
I'm trying to make a loop in wordpress with two different post types ('product' and 'outfits')
The code below is working fine, this outputs a list of the two post types ordered by newest to older.
$loop = new WP_Query( array(
'post_type' => array( 'product', 'outfits' ),
'posts_per_page' => 15
) );
$counter = 0;
?>
<?php if ( $loop->have_posts() ) { while ( $loop->have_posts() ) { $loop->the_post();
$counter++; ?>
<?php $loop->is_home = false; ?>
<?php
$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'large');
$titulo = get_the_title();
$content = apply_filters ('the_content', $post->post_content); ?>
<div class="caja-<?php echo $counter; ?>">
<?php if ( $post->post_type == "product" ) {
//some stuff
<?php } else {
//some stuff
} ?>
</div>
<?php } } wp_reset_postdata();
What I would like to do is to insert a product post type after X number of outfits.
I was trying to merge a similar solution I've found in other question with no luck. I'm not a php expert so any help is appreciated
<?php
$args = array('post_type'=>'post', 'posts_per_page'=>9, 'category_name'=>'news');
$posts = get_posts($args);
$args = array('post_type'=>'testimonials', 'posts_per_page'=>3);
$testimonials = get_posts($args);
// see how many of the regular posts you got back //
$post_count = count($posts);
// see how many testimonials you got back //
$testimonial_count = count($testimonials);
// add them up to get the total result count //
$total_count = $post_count + $testimonial_count;
// Loop through the total number of results //
for($i = 1; $i <= $total_count; $i++){
// assuming you want to show one testimonial every third post //
if($i % 3 == 0){
// this means you're on the a third post, show a testimonial //
setup_postdata($testimonials[$i]);
}
else{
/** show a regular post */
setup_postdata($posts[$i]);
}
/** and now handle the output */
?><h1><?php the_title();?></h1><?php
} ?>
$x = 3;
$products = get_posts(array(
'post_type' => 'product',
'posts_per_page' => -1
));
$outfits = get_posts(array(
'post_type' => 'outfit',
'posts_per_page' => -1
));
foreach ($outfits as $num => $outfit) {
if ( ($num+1) % $x == 0) {
if (isset($products[($num+1)/$x - 1])) {
array_splice( $outfits, $num, 0, array($products[($num+1)/$x - 1]) );
}
}
}
foreach ($outfits as $outfit) {
$posts_id[] = $outfit->ID;
}
$query = new wp_query(array(
'post_type' => array('outfit', 'product'),
'post__in' => $posts_id,
'posts_per_page' => 15,
'orderby' => 'post__in'
));
I am trying to insert a sorting option into my wordpress site. I already have it working, but need help using it with the wordpress loop correctly. Currently, I have:
On a page, there are options to sort alphabetically or chronologically:
Newest
Alphabetical
Sorting Code starts here, placed above the loop:
<?php $sort= $_GET['sort'];
if($sort == "title") { $order= "'orderby'=>'title','order'=>ASC'"; }
elseif($sort == "date") { $order= "'orderby'=>'date'"; }
else{ $order= "'orderby'=>'date','order'=>'DESC'"; }
?>
note: I am pretty sure the problem lies above in the variable $order
Wordpress Loop Using Variable $order as an argument
<?php $loop = new WP_Query( array( $order, 'post_type' => 'films', 'post_parent' => 0, 'posts_per_page' => -1 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
Wordpress loop stuff here
<?php endwhile; ?>
The loop displays items correctly, but the sorting links are not working. This code works very well with query_posts but I am trying to get this to work for WP_Query (above). Any ideas here?
UPDATE: This technique works great using query_posts like below (but I still need it working for WP_Query):
<?php $sort= $_GET['sort'];
if($sort == "title") { $order= "&orderby=title&order=ASC"; }
elseif($sort == "date") { $order= "&orderby=date"; }
else{ $order= "&orderby=date&order=DESC"; }
?>
<?php $posts = query_posts($query_string . $order); ?>
<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
Wordpress Stuff Here
<?php endwhile; ?>
<?php endif; ?>
WP_Query expects associative array of parameters (array('param' => 'value')), whereas query_posts accepts only "query strings" ("param=value¶m=value"). You are mixing both options, that's why it doesn't work. You need to change $order variable to be array instead of string, for example: $order = array('orderby' => 'title', 'order' => ASC');.
Answer above is not complete and may be misleading. WP_Query docs are here: https://codex.wordpress.org/Class_Reference/WP_Query . This class accepts boths styles, but they must be somewhat different formed. I don't know how exactly to do this, because it is not written in class docs, but you better use arrays, so:
if($sort == "title") { $order = array('orderby' => 'title', 'order' => 'ASC'); }
elseif($sort == "date") { $order= array('orderby' => 'date'); }
else{ $order= array('orderby' => 'date', 'order' => 'DESC'); }
FYI to convert between both types use functions parse_str(): http://php.net/manual/en/function.parse-str.php and http_build_query(): http://www.php.net/manual/en/function.http-build-query.php .