Trying to exclude featured posts from Wordpress post loop - php

Ive installed a featured post plugin "NS Featured Posts" which works quite nicely but has no known functionality for excluding those posts from another loop. Here's what I have so far:
<div id="all-news-block" class="container">
<div class="row">
<?php
$args = array(
'post_type' => 'post',
'posts_per_page'=>-1,
'meta_query' => array(
array(
'key' => '_is_ns_featured_post',
'value' => 'yes',
'compare' => 'NOT LIKE',
),
),
);
$query = new WP_Query( $args );
?>
<?php if ( $query->have_posts() ) : ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="news-item-block col-md-4" role="article">
<?php if (has_post_thumbnail( $post->ID ) ): ?>
<a class="news-item-image-link" href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('featured-news-item-image'); ?>
</a>
<?php endif; ?>
<span class="news-item-date"><?php echo get_the_date('M d, Y'); ?></span>
<a class="news-item-title" href="<?php the_permalink(); ?>">
<h1><?php the_title(); ?></h1>
</a>
</div>
<?php endwhile; endif; wp_reset_query(); ?>
</div>
Any help would be appreciated.

Since I cannot comment due to the lack of reputation, I will post this as an answer although I don't think is a final/suitable answer.
Focusing on the array you have in there:
array(
'key' => '_is_ns_featured_post',
'value' => 'yes',
'compare' => 'NOT LIKE',
),
I would rather change the third value (2) to this - as a regular php operator -:
'compare' => '!=',
Also, you could change the while loop in order to count after the number of featured posts you got.

Related

Query posts by ACF checkbox field

I cant seem to get facetwp to only display posts where an ACF checkbox field has a value of yes. My ACF checkbox value is stored in the post sidebar which I believe attaches it to the post itself (for lack of a more knowledgable way of explaining it)
There are lots of examples that are similar online, but I can't seem to modify any to work for me.
I have also tried with the ACF True/false field type, as it doesn't matter to me which type of field it is.
My latest attempt is
<div class="full-width-inner">
<div class="package-listings">
<div class="listings-table-row">
<?php
$type = get_field( 'is_this_educational', get_the_ID() );
$my_posts = new WP_Query([
'post_type' => 'packages',
'posts_per_page' => 3,
'orderby' => 'date',
'meta_query' => [
[
'key' => 'is_this_educational',
'value' => '1'
]
]
]);
if ( $my_posts->have_posts() ) {
while ( $my_posts->have_posts() ) : $my_posts->the_post(); ?>
<div class="package-wrapper edu_green_bg">
<?php if( get_field('add_flag', $post->ID)) { //Check if checkbox is checked ?>
<div class="flag orange-bg"><p><?php the_field( 'flag_text', $post->ID ); ?></p></div>
<?php } ?>
<?php if ( has_post_thumbnail() ) : ?>
<div class="package-home-image">
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_post_thumbnail(); ?>
</a>
</div>
<?php endif; ?>
<div class="result-info">
<div class="home-package-title">
<?php the_title(); ?>
</div>
<div class="home-package-excerpt">
<p><?php echo wp_trim_words( get_the_excerpt(), 20, '...' ); ?></p>
</div>
<div class="home-package-button orange-bg">
<button>Read More</button>
</div>
</div>
</div> <!-- package wrapper end -->
<?php
endwhile;
wp_reset_postdata();
}
?>
</div> <!-- listings table row end -->
</div> <!-- Package listings end -->
</div>
But I have also tried:
$type = get_field( 'is_this_educational', get_the_ID() );
$my_posts = new WP_Query([
'post_type' => 'packages',
'posts_per_page' => 3,
'orderby' => 'date',
'meta_query' => [
[
'key' => 'is_this_educational',
'value' => '"yes"'
]
]
]);
if ( $my_posts->have_posts() ) {
while ( $my_posts->have_posts() ) : $my_posts->the_post(); ?>
And
<?php
$type = get_field( 'is_this_educational', get_the_ID() );
$my_posts = new WP_Query([
'post_type' => 'packages',
'posts_per_page' => 3,
'orderby' => 'date',
'meta_query' => [
[
'key' => 'is_this_educational',
'value' => 'yes',
'compare' => 'LIKE'
]
]
]);
if ( $my_posts->have_posts() ) {
while ( $my_posts->have_posts() ) : $my_posts->the_post(); ?>
And lastly - which I think might be the closest of them all but gives me a critical error.
Sorry for all the code but just to demonstrate that I have been trying
<section class="full-width-package-filter full-width-section cream-bg">
<div class="full-width-inner">
<div class="package-listings">
<div class="listings-table-row">
<?php
$posts = get_posts(array(
'meta_query' => array(
array(
'key' => 'is_this_educational',
'value' => '"yes"',
'compare' => 'LIKE'
)
)
));
if( $posts ) {
while ( $my_posts->have_posts() ) : $my_posts->the_post(); ?>
<div class="package-wrapper edu_green_bg">
<?php if( get_field('add_flag', $post->ID)) { //Check if checkbox is checked ?>
<div class="flag orange-bg"><p><?php the_field( 'flag_text', $post->ID ); ?></p></div>
<?php } ?>
<?php if ( has_post_thumbnail() ) : ?>
<div class="package-home-image">
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_post_thumbnail(); ?>
</a>
</div>
<?php endif; ?>
<div class="result-info">
<div class="home-package-title">
<?php the_title(); ?>
</div>
<div class="home-package-excerpt">
<p><?php echo wp_trim_words( get_the_excerpt(), 20, '...' ); ?></p>
</div>
<div class="home-package-button orange-bg">
<button>Read More</button>
</div>
</div>
</div> <!-- package wrapper end -->
<?php
endwhile;
wp_reset_postdata();
}
endif;
?>
</div> <!-- listings table row end -->
</div> <!-- Package listings end -->
</div>
</section>

ACF row to show thumbnails of recent posts

I'm trying to create a ACF flexible content row to display the most recent post thumbnails for a given category. However it keeps throwing a critical error and I'm not sure why.
<?php
$section_id = get_sub_field('section_id')
$categories = get_sub_field('categories');
$tags = get_sub_field('tags');
$postnum = get_sub_field('number_of_posts');
if (!is_array($categories)) {
$categories = array($categories);
}
$tags = get_field('my_tags_field');
if (!is_array($tags)) {
$tags = array($tags);
}
$args = array(
'post_type' => 'post',
'numberposts' => $postnum,
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'category',
'terms' => $categories
),
array(
'taxonomy' => 'post_tag',
'terms' => $tags
)
)
);
$query = new WP_Query($args);
?>
<style>
</style>
<section class="post_row_with_thumbnails" id="<?php echo $section_id; ?>">
<div class="container-fluid">
<div class="row">
<?php if( $query->have_posts() ) : while( $query->have_posts() ) : $query->the_post(); ?>
<div class="col">
<a href="<?php the_permalink(); ?>">
<img src="<?php echo wp_get_attachment_url( get_post_thumbnail_id( $post->ID ) ); ?>" class="project_pics">
<h5 class="posttitle"><?php the_title(); ?></h5>
<h6 class="postdate"><?php the_date(); ?></h6>
</a>
</div>
<?php endwhile; endif; wp_reset_postdata(); ?>
</div>
</div>
</section>
I have tried substituting WP_Query() with get_posts() but it gives me the same critical error.
In line 2 the ; at the end of the line, in this part:
$section_id = get_sub_field('section_id') ; THE DOT AND COMMA IS MISSING
This is the reason for the critical error.

How do I query posts custom relationship field and include parsed search term into loop and display results in WordPress

I'm parsing a search term from the URL in a search results page like so:
http://localhost:8888/domain/?s=Varilite%20Icon%20Mid&post_type=knowledge_hub
I need the search.php template file to query a ACF relationship custom field to see if the linked post title matches the parsed variable in the URL and display the results. Below is my code but it does not work as it only displays posts with the search term in the post titles. How do I get my query to also check the custom field? Bare in mind this tempalte file is also used for the default global search so needs to be flexible.
<?php
$args = array(
'posts_per_page' => -1,
//'fields' => 'ids',
'post_type' => 'knowledge_hub',
'meta_query' => array(
array(
'key' => 'related_products',
'value' => '"'.get_search_query().'"',
'compare' => 'LIKE',
)
),
);
$relatedProductArticles = get_posts($args);
if ( have_posts($relatedProductArticles) ) :
while ( have_posts($relatedProductArticles) ) : the_post($relatedProductArticles);
?>
<article class="col-12 search-item mb-5">
<div class="row d-flex align-items-center">
<div class="col-md-7">
<?php the_post_thumbnail('medium', ['class' => 'w-100']); ?>
</div>
<div class="col-md-4">
<div class="px-4">
<h2><a class="" href="<?php the_permalink() ?>"><?php the_title() ?></a></h2>
<?php the_excerpt(); ?>
</div>
</div>
</div>
</article>
<?php
endwhile;
else : ?>
<h2><?php _e( 'Sorry, no posts matched your criteria.', 'textdomain' );?></h2>
<?php endif;
?>
TRY using $_GET.
<?php
$args = array(
'posts_per_page' => -1,
//'fields' => 'ids',
'post_type' => 'knowledge_hub',
'meta_query' => array(
array(
'key' => 'related_products',
'value' => $_GET['s'],
'compare' => 'LIKE',
)
),
);
$relatedProductArticles = get_posts($args);
if ( have_posts($relatedProductArticles) ) :
while ( have_posts($relatedProductArticles) ) : the_post($relatedProductArticles);
?>
<article class="col-12 search-item mb-5">
<div class="row d-flex align-items-center">
<div class="col-md-7">
<?php the_post_thumbnail('medium', ['class' => 'w-100']); ?>
</div>
<div class="col-md-4">
<div class="px-4">
<h2><a class="" href="<?php the_permalink() ?>"><?php the_title() ?></a></h2>
<?php the_excerpt(); ?>
</div>
</div>
</div>
</article>
<?php
endwhile;
else : ?>
<h2><?php _e( 'Sorry, no posts matched your criteria.', 'textdomain' );?></h2>
<?php endif;
?>

Wordpress page content and posts in the same page

I'm pretty new in world of wordpress and I'm trying to adjust HTML page into wordpress theme.
I need a page content to be displayed first on a page and under that, the posts should be shown. But what I'm getting are just posts shown twice on the page (where page content should be). Is there any possibility to overcome this?
And additional question, how to filter posts according to their category? I've tried with query_posts('cat=Small'), but it doesn't seem to work properly.
The code for index.php looks as following:
<?php get_header(); ?>
<?php
wp_reset_query();
while ( have_posts() ) : the_post();
the_content();
endwhile;
wp_reset_query();
?>
<section>
<header class="major">
<h2>Erat lacinia</h2>
</header>
<div class="features">
<?php query_posts('cat=Small'); ?>
<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
<article>
<span class="icon fa-diamond"></span>
<div class="content">
<h3><?php the_title(); ?></h3>
<p><?php the_content('Read More'); ?></p>
</div>
</article>
<?php endwhile; endif; ?>
<?php wp_reset_query(); ?>
<?php get_footer(); ?>
Try the below code. This may help you
<section>
<div class="major">
<h2>Erat lacinia</h2>
</div>
<div class="features">
<?php $args = array(
'posts_per_page' => -1,
'offset' => 0,
'category' => '',
'category_name' => '',
'orderby' => 'date',
'order' => 'ASC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'author' => '',
'post_status' => 'publish',
'suppress_filters' => true
);?>
<?php query_posts( $args ); ?>
<?php while ( have_posts() ) : the_post(); ?>
<article>
<span class="icon fa-diamond"></span>
<div class="content">
<h3><?php the_title(); ?></h3>
<p><?php the_content('Read More'); ?></p>
</div>
</article>
<?php endwhile; wp_reset_query(); ?>
</div>
</section>
You can use two loops.
In your php page template, first execute the regular loop to get the content of the actual page, like this:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
//output page content here
<?php endif; ?>
Then you define a new query for the desired posts:
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 3,
'orderby' => 'date',
'order' => 'ASC',
)
);
//(Add and change arguments as desired in the array above)
$loop1 = new WP_Query($args);
if ( $loop1->have_posts() ) : while ( $loop1->have_posts() ) : $loop1->the_post();
//Output the post contents in a loop here
<?php endif;
wp_reset_postdata();?>
And then add the rest of the page template (footer etc.)
<?php
/*
*Template name: test
*/
get_header();
if ( have_posts() ) :
while ( have_posts() ) : the_post();
$attrs = array(
'numberposts' => 10,
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array( 'small' )
)
)
);
$my_posts = get_posts( $attrs );
the_content();
?>
<?php if ($my_posts): ?>
<section>
<header class="major">
<h2>Erat lacinia</h2>
</header>
<div class="features">
<?php foreach ($my_posts as $key => $value): ?>
<article>
<span class="icon fa-diamond"></span>
<div class="content">
<h3><?= $value->post_title; ?></h3>
<p><?= $value->post_content ?></p>
</div>
</article>
<?php endforeach ?>
</div>
</section>
<?php endif ?>
<?php
endwhile;
else :
echo wpautop( 'Sorry, no posts were found' );
endif;
get_footer(); ?>

post by taxonomy not showing at all in wordpress

Newbie here,
I'm trying to display the list of my post depends on the category. But it doesn't display my post. I tried the different type of array, but no luck. I named my page taxonomy-blog_category.php
I call the page via site.com/blog_category/category
here's my current code and I know I'm so close but can't figure it out.
Here is the array:
<div class="row">
<?php $ctr = 1;
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$custom_args = array(
'post_type' => 'blog_post',
'orderby' => 'date',
'order' => 'DESC',
'post_status' => 'publish',
'posts_per_page' => 6,
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'blog-category',
'field' => 'slug',
'terms' => array('business','people','technology'),
),
),
);
Here is how I display the post
$custom_query = new WP_Query( $custom_args ); ?>
<?php if ( $custom_query->have_posts() ) : ?>
<?php while ( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
<div class="col-md-4 text-center">
<div class="content-container">
<div class="wrap">
<figure class="tint t2">
<img src="<?php echo wp_get_attachment_url( get_post_thumbnail_id() ); ?>" width="317px" height="240">
</figure>
</div>
<h2><?php the_title(); ?></h2>
<h3>By <?php the_field('author'); ?> | <span><?php echo get_the_date(); ?></span></h3>
<?php $content = get_field('content'); echo mb_strimwidth($content, 0, 200, '...');?>
<div class="read-more-btn">
read more
</div>
</div>
</div>
<?php $ctr++; endwhile; ?>
I Don't know if this is necessary but here's my code for pagination:
<div class="pagination-holder">
<ul class="pagination">
<?php
if (function_exists(custom_pagination)) {
custom_pagination($custom_query->max_num_pages,"",$paged);
}
?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
</ul>
</div>

Categories