I'm trying to apply a php code snippet to my wordpress page to enable Jetpack to limit its related posts to no later than 2017-01-01. However, when I add this code via Snippets - with Jetpack enabled to show thumbnails - Jetpack doesn't work. Is my code incorrect or is there some magic trick for this?
// related posts - don't show posts earlier than January 1 2017
function jetpackme_exclude_related_post($exclude_post_ids, $post_id ) {
$args = array(
'date_query' => array(
'before' => 2017-01-01,
'inclusive' => true,
),
'posts_per_page' => '99999',
'fields' => 'ids'
);
$query = new WP_Query($args);
if ($query->have_posts()):
foreach($query->posts as $id ):
$exclude_post_ids[] = $id;
endforeach;
endif;
return $exclude_post_ids;
}
add_filter( 'jetpack_relatedposts_filter_exclude_post_ids',
'jetpackme_exclude_related_post', 20, 2 );
Related
I am creating a blog and I have difficulty with pages in wordpress, in my case I would like to insert content (raw content) from two pages randomly in modal system while people browse the blog.
Example:
Current Page> Modal Box> Page01 content or Page02 Content
I know it's possible by the page id. but I would like to make it more dynamic by getting the contents of the pages by slugs or by title.
here's the code
<?php
$term = get_taxonomy( $slug );
$args = array(
'post_type' => 'page',
'posts_per_page' => 1,
'orderby' => 'RAND',
'tax_query' => array( array(
'taxonomy' => $term,
'field' => 'slug',
'terms' => array('page-01', 'page-02'),
) )
);
$rand = new WP_Query($args);
if ($rand->have_posts()) {
while ($rand->have_posts()) {
$rand->the_post();
the_content();
}
}
?>
I know that removing taxonomy from query gets the contents of publish pages but, in my case, I need to get content from two specific pages
You can use post_name__in to filter by multiple slugs, eg:
$args = array(
'post_name__in' => array('page-slug-1', 'page-slug-2'),
'post_type' => 'page',
'posts_per_page' => 1,
'orderby' => 'RAND',
);
You can find more details on the WP_Query documentation page.
Hi I have created my own custom post type within Wordpress to contain projects that i can call via my theme files.
I am new to creating my own themes. I currently am using the following code in my single.php file to call in related articles based on the category of the blog post.
<?php
// Default arguments
$args = array(
'posts_per_page' => 3, // How many items to display
'post__not_in' => array( get_the_ID() ), // Exclude current post
'no_found_rows' => true, // We don't ned pagination so this speeds up the query
);
// Check for current post category and add tax_query to the query arguments
$cats = wp_get_post_terms( get_the_ID(), 'category' );
$cats_ids = array();
foreach( $cats as $wpex_related_cat ) {
$cats_ids[] = $wpex_related_cat->term_id;
}
if ( ! empty( $cats_ids ) ) {
$args['category__in'] = $cats_ids;
}
// Query posts
$wpex_query = new wp_query( $args );
// Loop through posts
foreach( $wpex_query->posts as $post ) : setup_postdata( $post ); ?>
<div class="col-md-4 related-post">
<?php the_post_thumbnail('large'); ?>
<?php the_title(); ?>
</div>
<?php
// End loop
endforeach;
// Reset post data
wp_reset_postdata(); ?>
In my new post type "projects" i would like to call in related projects. Which im assuming would be very similar code except i need to stop it looking for posts and instead look for my projects.
Here is my code for new post type:
// Projects
add_action( 'init', 'create_post_type' );
add_post_type_support( 'bw_projects', 'thumbnail' );
add_post_type_support( 'bw_projects', 'custom-fields' );
function create_post_type() {
register_post_type( 'bw_projects',
array(
'labels' => array(
'name' => __( 'Projects' ),
'singular_name' => __( 'Projects' )
),
'public' => true,
'has_archive' => true,
'taxonomies' => array( 'category','post_tag'),
)
);
}
What would i need to change in my first code snippet in order to look for bw_projects and not look for 'posts' anymore. I tried playing around and changing certain lines myself but i caused more issues and stopped the page loading. Is this even right i can use the same code, slightly altered or would i need something completely different?
Thanks in advance.
You can get any post type that you require using get_posts();
<?php $args = array(
'posts_per_page' => 5,
'offset' => 0,
'category' => '',
'category_name' => '',
'orderby' => 'date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'projects',
'post_mime_type' => '',
'post_parent' => '',
'author' => '',
'author_name' => '',
'post_status' => 'publish',
'suppress_filters' => true
);
$posts_array = get_posts( $args ); ?>
Simply set the 'post_type' argument to that of you custom post type, to get only these posts. You can also set the number of post, and filter by category etc.
You can find more info in the codex.
Alternatively, if you wanted to keep something similar to your existing code you could try using 'pre_get_posts' to filter the query to just your projects. However you'd need to remember to add / remove this filter so it only operates on the queries that need it.
To display the posts you can use a simple foreach to churn them out. You#d obviously want to do some sort of styling to get the layout correct:
$args = array("posts_per_page" => 10, "orderby" => "comment_count");
$posts_array = get_posts($args);
foreach($posts_array as $post)
{
echo "<h1>" . $post->post_title . "</h1><br>";
echo "<p>" . $post->post_content . "</p><br>";
}
Or a really concise way of doing all of the above would be something like:
$args = array("posts_per_page" => 5, "post_type" => "projects");
$posts_array = get_posts($args);
foreach($posts_array as $post)
{
echo "<h1>" . $post->post_title . "</h1><br>";
echo "<p>" . $post->post_content . "</p><br>";
}
I've added the ability to choose from custom post types in the woocommerce product admin tabs with a function as used in this tutorial http://www.remicorson.com/mastering-woocommerce-products-custom-fields/
so I've added a custom field
woocommerce_wp_select(
array(
'id' => '_circuit',
'label' => __( 'choose circuit', 'woocommerce' ),
'options' => get_circuits_as_array()
)
);
now the function looks like this
function get_circuits_as_array(){
$args = array( 'post_type' => 'top', 'posts_per_page' => -1, 'post_status'=>'published' );
$loop = new WP_Query( $args );
$circuits = array('0'=>'--wybierz opcję--');
while ( $loop->have_posts() ) : $loop->the_post();
setup_postdata( $post );
$circuits[get_the_id()] = get_the_title();
endwhile;
wp_reset_query();
return $circuits;
}
The problem is that while uploading the code to the server this function breaks the variations window it shows only the default "add variations message"
The console shows no errors.
I guess this has something to do with ajax requests but cant figure out exactly what, I've tries to move the get function in other files etc. but no luck.
The woocommerce plugin version is 2.2.8
OK so I've figured this out and the workaround is to use a foreach loop with $loop->posts as the array
function get_circuits_as_array(){
$args = array( 'post_type' => 'top', 'posts_per_page' => -1, 'post_status'=>'published' );
$loop = new WP_Query( $args );
$circuits = array('0'=>'--wybierz opcję--');
foreach ($loop->posts as $circuit) {
$circuits[$circuit->ID] = $circuit->post_title;
}
wp_reset_query();
return $circuits;
}
I need to have 5 random posts on post page (http://7cuteoutfits.com/2015/07/08/suits-rachel-z-office-fashion/) excluding current post. Random posts should be on chosen dates (for example posts from last 2 months until yesterday )
I added a few lines of code to single.php of my wordpress and now have 5 random posts. So I need to modify the code so that it will meet my requirements (above). I think it's 2 more lines, I'll be very thankful if you help.
<ul>
<?php
$currentID = get_the_ID();
$args = array( 'posts_per_page' => 5, 'orderby' => 'rand' );
$rand_posts = get_posts( $args);
foreach ( $rand_posts as $post ) :
setup_postdata( $post ); ?>
<li><?php the_title(); ?></li>
<?php endforeach;
wp_reset_postdata(); ?>
</ul>
You can use WP_Query for that.
global $post;
$args = array(
'post__not_in' => array($post->ID)
'orderby' => 'rand'
'date_query' => array(
array(
'after' => 'January 1st, 2015',
'before' => array(
'year' => 2015,
'month' => 07,
'day' => 9,
),
'inclusive' => true,
),
),
'posts_per_page' => 5,
);
$query = new WP_Query( $args );
This query orders randomly posts between today (inclusive) and Jan. 1st 2015
I haven't tested the snippet here, so please let me know if it does not work for you.
More info on WP_query and its usage (also for date parameters) here
Once you query with WP_Query, you have to
wp_reset_postdata();
just as you are already doing.
EDIT:
To show the post content, you can call
the_content()
to print it directly, or
get_the_content()
to get it as a return value. Then you can handle the printing later with the HTML markup you desire.
I'm having some trouble displaying a list of posts from a Wordpress Category that will exclude a certain number of post based on a custom field using Advance Custom Fields.
Here's the current code I'm using that hides it nicely:
while ( have_posts() ) : the_post();
$is_taken = get_field('taken_check', $this_id);
if ($is_taken!=1) {
get_template_part( 'basket_selection' );
}
endwhile;
However, it simply just hides the post but still considers it as a post on the "posts_per_page" function.
For example, There are 20 posts in total and I've set the limit to 10 posts per page. If I hide 3 posts with the code above, it will only display 7 posts in page 1 and 10 posts in page 2.
Is there a way to simply just ignore the hidden posts and not count it as a "post"?
Try this:
Apply Custom Fields Parameters in get_post query itself.
$posts = get_posts(array(
'posts_per_page' => 10,
'post_type' => '<YOUR_POST_TYP>',
'meta_key' => 'taken_check',
'meta_value' => '<DEFAULT_VALUE_OF_taken_check>'
));
Lots to read here: http://codex.wordpress.org/Template_Tags/get_posts
I've managed to solve it by changing the get_posts to wp_query within the category.php.
I first added this code to detect the current category viewed and filter the query to only display taken_check = 0.
$this_cat = get_category(get_query_var('cat'), 'ARRAY_A', false);
foreach ($this_cat as $this_cat){
$this_catid = $this_cat;
break;
}
$args = array(
'posts_per_page' => 10,
'post_type' => 'post',
'cat' => $this_catid,
'orderby' => 'title',
'order' => 'ASC',
'paged' => $paged,
'meta_query' => array(
array(
'key' => 'taken_check',
'value' => '0',
)
)
);
$wp_query = new WP_Query($args);
I then just continued with the default loop sequence. The only weird code is the unnecessary foreach loop to detect the current category based on the current page and not from a post. Still puzzled as to why I can't just use $this_cat[0] since it's an array. It keep returning blank.
Oh well, but it works now with pagination, so I'm happy :)
Thanks for all the help!