Specify the first post in while loop by taxonomy - php

I'm trying to query posts in wordpress, so far so good. However, I would like to show a post with a specific tag 'info' in front of all (even in front of sticky).
Now the query of the theme is like this:
$sticky = get_option( 'sticky_posts' );
rsort( $sticky );
if(of_get_option('sticky-posts') == 'show_sticky'){
query_posts (array( 'post__in' => $sticky, 'order' => $order_posts, 'ignore_sticky_posts' => 1, 'paged' => $paged ) );
}
And after that the loop starts:
//BEGIN LOOP
//=====================================================?>
<article id="articlehold">
<?php
if(have_posts()) : while ( have_posts() ) : the_post();
Now I've done some editing in the query to get the post with the 'info' tag in front, but no luck. Now I'm not sure where the best place is to edit this, in the loop or before the loop?
How could I do this?

Related

on my homepage I want to show a "news" section with posts tagged "news" from the last 30 days. How?

As the title says, in the homepage (index.php) I want to add a section for the news which is populated by "news" tagged posts and it only shows posts from the last 30 days. if no posts has been published in the last 30 days then the news section is empty and disappears.
how do I query those posts from last 30 days? do I use a new WP_query or pre_get_posts?
how do I write it to make the section not show up if there are no news?
I'm guessing it's enough to create the div inside the loop instead of outside using
if ( have_posts() ) :
echo '<div class="newsdiv">','<ul>'
while ( have_posts() ) : the_post('<li>','</li>');
// Display post content
the_title('<h2 class="news">','</h2>');
the_thumbnail();
the_excerpt();
endwhile;
echo '</ul>','</div>'
endif;
?>
i'm sorry for banal questions or mistakes, i'm trying to learn wordpress and php. If you could add some explanation with your answer it would make it easier for me to learn the why and not just how.
Using WP_Query add next params:
$args= array(
'date_query' => array(
array(
'after' => '-30 days',
'column' => 'post_date',
),
),
);
$your_query = new WP_Query($args);
It will grab posts post_type => 'post' for the last 30 days
if($your_query->have_posts()) :
// Code to display your news
while($your_query->have_posts()) : $your_query->the_post();
// Single news
endwhile; wp_reset_postdata();
else:
// No posts
endif;
Using WP_Query like this:
$args = array(
'post_type' => 'posts',
'posts_per_page' => 20,
'date_query' => array(
array(
'after' => '-30 days',
'column' => 'post_date',
),
),
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) :
while ( $loop->have_posts() ) : $loop->the_post();
// add your loop content here
endwhile;
wp_reset_postdata();
// Reset query incase we want to use another query on the same page
endif;

How can I exclude only latest post in certain category from showing in loop?

I have two loops and queries.
At the top of page I have this code. It only shows the latest post from category named "featured":
<?php
$latest_featured_post = new WP_Query ( array ( 'category_name' => 'featured', 'posts_per_page' => 1 ) );
while ($latest_featured_post->have_posts()) : $latest_featured_post->the_post();
?>
Now I want to exclude that post from the other, main, loop on the same page, because I don't want it to show twice. I tried to achieve that by catching the ID of a latest post in a "featured" category and passing it to the 'post__not_in' argument but I did something wrong. This is my code
<?php
$category_id = get_cat_ID('Događaji');
$exlude_latest_featured_post = array($latest_featured_post->ID);
$args = array(
'category__not_in' => array($category_id),
'post__not_in' => $exlude_latest_featured_post,
);
query_posts( $args );
while (have_posts()) : the_post(); ?>
<?php get_template_part('loop/content'); ?>
I tried to manually pass ID of the post ('post__not_in' => array(1337) for example) and it works. Which means that I made mistake with catching the "featured" latest post ID.
I was searching Google for the answer but I didn't find anything helpful. Hope someone here has time and right answer
Thanks
You can capture the featured post id withing the 1st loop via get_the_id function, then use it in the later loop:
<?php
$latest_featured_post = new WP_Query ( array ( 'category_name' => 'featured', 'posts_per_page' => 1 ) );
while ($latest_featured_post->have_posts()) :
$latest_featured_post->the_post();
$featuredID = get_the_id();
?>
Your latter loop:
$category_id = get_cat_ID('Događaji');
$exlude_latest_featured_post = array($featuredID);

Edit post arg for specific Wordpress category

I have this in my loop:
<?php
$args=array(
'cat' => $select_blog_category,
'orderby' => 'date',
'order' => 'DESC',
'paged' => $paged
);
$temp = $wp_query;
$wp_query = new WP_Query( $args );
// The Loop
while ( $wp_query->have_posts() ) : $wp_query->the_post();
$featured_image_array = wp_get_attachment_image_src( get_post_thumbnail_id(), 'blog-square' );
$featured_image = $featured_image_array[0];
?>
I want this loop to display a specific category. I thought I could do this by changing:
'cat' => $select_blog_category
to this (my post category):
'cat' => 'upcoming-2'
But it doesn't work. I love everything else about how this code is displaying my posts, just want to filter it, thanks!
I also need to add another loop on the same page with a different category. Can I have two loops on the same page? So far every attempt I have tried at copying the code to a new section has resulted in a black white page, which tells me I'm doing something wrong.
I only come here as a last resort, not because I want someone to do my work, so I really appreciate the help, thanks!

Wordpress query to show only sticky posts

Following is my wordpress query in which i want to show only sticky posts but the query is not showing any posts. Also I set two posts as sticky so that part is checked!!!. Kindly let me know how to modify this query so it will only show the posts which are sticky
<?php
$wp_query = null;
$wp_query = new WP_Query(array(
'posts_per_page' => 2,
//'paged' => get_query_var('paged'),
'post_type' => 'post',
'post__in' => 'sticky_posts',
//'post__not_in' => array($lastpost),
'post_status' => 'publish',
'caller_get_posts'=> 0 ));
while ($wp_query->have_posts()) : $wp_query->the_post(); $lastpost[] = get_the_ID();
?>
Query which will show only sticky posts:
// get sticky posts from DB
$sticky = get_option('sticky_posts');
// check if there are any
if (!empty($sticky)) {
// optional: sort the newest IDs first
rsort($sticky);
// override the query
$args = array(
'post__in' => $sticky
);
query_posts($args);
// the loop
while (have_posts()) {
the_post();
// your code
}
}
The query_posts() function creates a new WP_Query() not before the current query is set up, meaning this is not the best efficient method and will perform extra SQL requests.
Use the 'pre_get_posts' hook to be safe, like
function sticky_home( $query ) {
$sticky = get_option('sticky_posts');
if (! empty($sticky)) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'post__in', $sticky );
}
}
} add_action( 'pre_get_posts', 'sticky_home' );

WordPress: Custom Query

I have a custom query that uses an array merge so I can list articles and custom post type posts in one list, showing 5 at a time. Pagination below allows the user to see the rest of the results. I keep getting pagination errors, but I've tried virtually every variation of the $paged variable in my query to get pagination to work and it doesn't. I know it's me, and probably a simple syntax thing...but I'm stumped. Any ideas? (Note: this page has multiple, other custom queries above the one in question)
Here's my code:
<?php
$loop1 = array_merge( $wp_query->query,
array( 'post_type' => array('post','podcasts', 'cat' => $cat_ID ),
'paged' => ( get_query_var('page') ? get_query_var('page') : 1 ),
'posts_per_page' => 5 ) );
query_posts( $loop1 );
while (have_posts()) : the_post(); ?>
have you tried?
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1 ),

Categories