Is it possible to get an array of all wordpress child page IDs that are no using a certain template?
I know I can get the template with:
'meta_key' => '_wp_page_template',
'meta_value' => 'template.php'
and I expect I could use get_pages, but not sure how I can put the query together.
This works, but is there a better way of doing it?
$args = array(
'post_parent' => $parent_ID,
'post_type' => 'page',
'numberposts' => -1,
'post_status' => 'publish'
);
$children = get_children( $args );
$exclude_children = null;
foreach($children as $child) {
$metadata = get_post_meta($child->ID);
if($metadata['_wp_page_template'][0] == 'template.php') {
$args = array(
'post_parent' => $child->ID,
'post_type' => 'page',
'numberposts' => -1,
'post_status' => 'publish'
);
$exclude_array = get_children( $args );
foreach($exclude_array as $exclude) {
$exclude_children .= $exclude->ID.',';
}
}
}
Related
I want to get some products of an post_author from DB. The problem is that get_posts is not working properly, from my debug that's a problem from the theme that I am using.
Is there any alternative to get products from DB?
What I tried:
-----1----
$related_products = get_posts( array(
'post_type' => 'product',
'author' => 19,
'post_status' => 'publish',
) );
-----2----
$product_query = new WP_Query( array(
'author' => 19,
'post_type' => 'product',
'post_status' => 'publish',
) );
$related_products = $product_query->posts;
-----3----
$args = array_merge( $wp_query->query_vars, array(
'post_type' => 'product',
'author' => 19,
) );
$GLOBALS['wp_query'] = new WP_Query();
$related_products = $GLOBALS['wp_query']->query( $args );
I get results, but random products.
I am making a function in a plugin and the function will delete the database row when the post move to the trash bin. However, I cannot get the post_id using get_posts().
Here is my code:
function delete_condition($post)
{
global $wpdb;
$allposts = get_posts(array(
'numberposts' => -1,
'category' => 0, 'orderby' => 'date',
'order' => 'DESC', 'include' => array(),
'exclude' => array(), 'meta_key' => '',
'meta_value' =>'', 'post_type' => 'job',
'suppress_filters' => true));
foreach( $allposts as $postinfo ) {
$wpdb->delete('rule', array('post_id' => $postinfo));
}
}
add_action('wp_trash_post', 'delete_condition', 10, 1);
thanks
The action hook you're using here, wp_trash_post, passes the $post_id to the function as a parameter. See: https://codex.wordpress.org/Plugin_API/Action_Reference/trash_post
It sounds like you want to delete all rows from one table that have the same post ID as the one being trashed.
I think you might want to write something like this:
function delete_condition( $post_id ) {
global $wpdb;
// Delete rows in the rule table which have the same post_id as this one
if ( 'job' === get_post_type( $post_id ) ) {
$wpdb->delete('rule', array('post_id' => $post_id ) );
}
}
add_action('wp_trash_post', 'delete_condition', 10, 1);
$postinfo is the object. You want only the ID of post. So you should write $postinfo->ID. Replace your loop with the below -
foreach( $allposts as $postinfo ) {
$postinfoID = $postinfo->ID;
$wpdb->delete('rule', array('post_id' => $postinfoID));
}
<?php
function delete_condition($post)
{
global $wpdb;
$allposts = get_posts(array(
'numberposts' => -1,
'post_status' => 'any',
'category' => 0, 'orderby' => 'date',
'order' => 'DESC', 'include' => array(),
'exclude' => array(), 'meta_key' => '',
'meta_value' =>'', 'post_type' => 'job',
'suppress_filters' => true));
foreach( $allposts as $postinfo ) {
$wpdb->delete('rule', array('post_id' => $postinfo));
}
}
add_action('wp_trash_post', 'delete_condition', 10, 1);
?>
I have kept a Prev-Next option in the single post page to navigate though the posts and this is what I'm using for the next button.
<?php echo get_permalink(get_adjacent_post(false,'',false)); ?>
But I can't figure out a way to link this same button to the very first posts when there are no more new posts to show. The reason is that the posts are used to show products.
Please note: To link Prev button in oldest post to the newest post I have used used this code.
<?php $next_page=get_permalink(get_adjacent_post(false,'',true));
$current_page=get_permalink();
if($next_page==$current_page){
$args = array( 'numberposts' => '1', 'category' => CAT_ID );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
echo get_permalink($recent["ID"]);
}
} else {
echo $next_page;
}
?>
add the args to query whatever posts are part of the products.
$posts_array = get_posts( $args );
get_permalink($posts_array[0]->ID); // First posts;
$args should be something like this (make sure it returns all the products posts):
$args = array(
'offset' => 0,
'category' => '',
'category_name' => '',
'orderby' => 'ASC',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'post_status' => 'publish',
'suppress_filters' => true
);
Their are 6 total queries I need to use, below is list of 2 of them. Is it possible to simplify it? As you can see the key is the different values of meta_value, they are 1-6.
<?php
$args1 = array(
'posts_per_page' => -1,
'post_parent' => $post->ID,
'post_type' => 'page',
'orderby' => 'date',
'order' => 'ASC',
'meta_key' => 'product_column',
'meta_value' => 1
);
$the_query1 = new WP_Query($args1);
if ($the_query1->have_posts()) {
while ($the_query1->have_posts()) {
$the_query1->the_post();
//do stuff
}
}
wp_reset_postdata();
?>
<?php
$args2 = array(
'posts_per_page' => -1,
'post_parent' => $post->ID,
'post_type' => 'page',
'orderby' => 'date',
'order' => 'ASC',
'meta_key' => 'product_column',
'meta_value' => 2
);
$the_query2 = new WP_Query($args2);
if ($the_query2->have_posts()) {
while ($the_query2->have_posts()) {
$the_query2->the_post();
//do stuff
}
}
wp_reset_postdata();
?>
If you just want to get all the posts with meta values 1-6, you can pass as array
$args1 = array(
'posts_per_page' => -1,
'post_parent' => $post->ID,
'post_type' => 'page',
'orderby' => 'date',
'order' => 'ASC',
'meta_key' => 'product_column',
'meta_value' => array( 1,2,3,4,5,6 )
);
$the_query1 = new WP_Query($args1);
Edited as per comments:
You could add the values to an array and run a foreach loop
$meta_values = array( 1,2,3,4,5,6 );
foreach ( $meta_values as $meta_value ) {
$args = array(
'posts_per_page' => -1,
'post_parent' => $post->ID,
'post_type' => 'page',
'orderby' => 'date',
'order' => 'ASC',
'meta_key' => 'product_column',
'meta_value' => $meta_value
);
$the_query = new WP_Query($args);
if ($the_query->have_posts()) {
while ($the_query->have_posts()) {
$the_query->the_post();
//...
}
}
wp_reset_postdata();
}
Basically I am trying to get the gallery of parent page, and I've been playing with query_posts to do that, the following code seems to be getting me closer to what I need but it is actually getting the attachments from other places rather than only parent page of current page, any one?:
<?php
$args = array('post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => 0,
'order_by' => 'menu_order',
'order' => 'ASC');
$attachments = get_posts($args);
if($attachments)
{
echo '<ul class="imagelist">';
foreach($attachments as $attachment)
{
echo '<li>';
$large = wp_get_attachment_image_src($attachment->ID, 'large');
$thumb = wp_get_attachment_image($attachment->ID, 'thumbnail');
echo '' . $thumb . '';
echo '</li>';
}
echo '</ul>';
}
?>
You should have set post_parent to the current post parent ID:
global $post;
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => $post->post_parent,
'order_by' => 'menu_order',
'order' => 'ASC'
);