Get pages with specific custom field value not working - wordpress

I have created a custom field in some pages and I need to loop through these pages and print their info. The code I'm using isn't working (the foreach is not looping).
Here's the code:
<?php
$args = array(
'meta_key' => 'categoria-pagina',
'meta_value' => 'programas'
);
$pages = get_pages($args);
foreach ($pages as $page) {
echo "<p>$page->post_title</p>";
}
wp_reset_postdata();
?>
And here's the page custom field config (wordpress in portuguese):
What's wrong with it?

Solved with this code:
<?php
$args = array(
'post_type' => 'page',
'meta_key' => 'categoria-pagina',
'meta_value' => 'programas'
);
$myPages = new WP_Query($args);
while ($myPages->have_posts()) : $myPages->the_post();
echo "$post->post_title";
endwhile;
wp_reset_postdata();
?>

<?php
$args = array(
'meta_key' => 'categoria-pagina',
'meta_value' => 'programas'
);
$custom_query = new WP_Query( $args );
// The Loop
if ( $custom_query->have_posts() ) {
while ( $custom_query->have_posts() ) {
$custom_query->the_post();
echo '<p>' . get_the_title() . '</p>';
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
That should do the trick for you.

Related

WordPress woo commerce get products by brand

In my WordPress project am trying to show products list based on brand name
This is my folder structure. Here am creating a API inside android folder.
In android/brands_products.php i want to show product list by brand name.
I tried this code:
<?php
require_once( '../wp-load.php' );
if ( woocommerce_product_loop() ) {
woocommerce_product_loop_start();
if ( wc_get_loop_prop( 'total' ) ) {
while ( have_posts() ) {
the_post();
/**
* Hook: woocommerce_shop_loop.
*
* #hooked WC_Structured_Data::generate_product_data() - 10
*/
do_action( 'woocommerce_shop_loop' );
wc_get_template_part( 'content', 'product' );
}
}
}
?>
But not working.
example: Pbs is brand name want to select Pbs brand name products
Please use below code :
<?php
require_once('../wp-load.php');
global $woocommerce;
global $product;
$brand_product_args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'post_status' => 'publish',
'product_cat' => 'pbs',
'order' => 'desc',
'orderby' => 'date'
);
$brand_product_list = new WP_Query( $brand_product_args);
while($brand_product_list->have_posts()) : $brand_product_list->the_post();
$product_data = wc_get_product( $post->ID );
endwhile; wp_reset_query();
if(!empty($product_list))
{
$data['status']= true;
$data['product']= $product_data;
}
else
{
$data['status']= false;
$data['product']= array();
}
echo json_encode($data);
?>
require_once('../wp-load.php');
global $woocommerce;
global $product;
$brand_product_args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'post_status' => 'publish',
'pwb-brand' => 'pbs',
'order' => 'desc',
'orderby' => 'date',
);
$brand_product_list = new WP_Query( $brand_product_args);
while($brand_product_list->have_posts()) : $brand_product_list->the_post();
$product_data = wc_get_product( $post->ID );
endwhile; wp_reset_query();
if(!empty($product_data))
{
$data['status']= true;
$data['product']= $product_data;
}
else
{
$data['status']= false;
$data['product']= array();
}
echo'<pre>'; print_r($data);exit;
echo json_encode($data);

Trying to show a user's custom post type posts in their buddypress profile?

-I have successfully added a new tab to the Buddypress profile page and I also have my custom tab displaying my custom recipes template located in (buddypress/members/single/recipes.php)
function my_setup_nav() {
global $bp;
bp_core_new_nav_item( array(
'name' => __( 'My Recipes', 'buddypress' ),
'parent_slug' => $bp->profile->slug,
'slug' => 'recipes',
'position' => 30,
'screen_function' => 'my_item_one_template',
'default_subnav_slug' => 'recipes'
) );
}
add_action( 'bp_setup_nav', 'my_setup_nav' );
function my_item_one_template() {
add_action( 'bp_template_content', 'my_item_create_screen' );
bp_core_load_template( apply_filters( 'bp_core_template_plugin', 'members/single/plugins' ) );
}
function my_item_create_screen() {
locate_template( array( 'buddypress/members/single/recipes.php' ), true );
}
?>
Now I want to list all the users custom recipe posts in their profile page, so that other users can see what recipes the user they are currently viewing has created. This is my current code but it does not work properly. It just displays all recipes, not just the ones from that particular user.
<?php
$type = 'recipe';
$args = array (
'post_type' => $type,
'author' => $bp->displayed_user->id,
'post_status' => 'publish',
'paged' => $paged,
'posts_per_page' => 10,
'ignore_sticky_posts'=> 1
);
$temp = $wp_query; // assign ordinal query to temp variable for later use
$wp_query = null;
$wp_query = new WP_Query($args);
if ( $wp_query->have_posts() ) :
while ( $wp_query->have_posts() ) : $wp_query->the_post();
echo '<h2>' . get_the_title() . '</h2>';
echo '<div class="entry-content">';
the_content();
echo '</div>';
endwhile;
else :
echo '<h2>Not Found</h2>';
get_search_form();
endif;
$wp_query = $temp;
?>
You're trying to use $bp without including it as a global.
But you don't need it.
Try changing
'author' => $bp->displayed_user->id,
to
'author' => bp_displayed_user_id(),

how to get related posts list in home page

how can i get list of latest posts with their relative posts by tags?
example:
Latest post post title 1
related post
related post
Latest post post title 2
related post
related post
use this in Index.php
<?php
$args = array(
'numberposts' => 100,
'offset' => 0,
'category' => 0,
'orderby' => 'post_date',
'order' => 'DESC',
'include' => ,
'exclude' => ,
'meta_key' => ,
'meta_value' =>,
'post_type' => 'post',
'post_status' => 'draft, publish, future, pending, private',
'suppress_filters' => true );
$recent_posts = wp_get_recent_posts( $args, ARRAY_A );
?>
<ul>
<?php
foreach( $recent_posts as $recent )
{
echo '<li>'.$recent["post_title"];
$tags = wp_get_post_tags($recent["ID"]);
if ($tags)
{
$first_tag = $tags[0]->term_id;
$args=array(
'tag__in' => array($first_tag),
'post__not_in' => array($recent["ID"]),
'posts_per_page'=>100,
'caller_get_posts'=>1
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() )
{
echo '<ul>';
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<li><?php the_title(); ?></li>
<?php
endwhile;
echo '</ul>';
}
wp_reset_query();
}
echo '</li>';
}
?>
</ul>
you can do something like this
your main loop contain recent post so during each loop use following function to get it's tag
$tag_ids = wp_get_post_tags( $post->ID, array( 'fields' => 'ids' ) );
then you can use another loop of using those tags
$query = new WP_Query( 'tag_id='.$tag_ids );
now $query has content you want.

Wp query in meta_box breaks wp editors

I just added a meta_box to my custom post_type in wordpress and my two extra wp_editors just dissapears.
I have code like this:
$args = array(
'post_type' => 'attachment',
'post_status' => 'any',
'posts_per_page' => -1,
'post_mime_type' =>'application/pdf, image/jpeg, image/gif, image/jpg, image/png'
);
$tmp = $post;
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
//some code for showing result etc...
}
}
$post = $tmp;
wp_reset_postdata();
I have narrowed it down to this 'post_status' => 'any'
If I change post status to something else, like:
post, page, custom etc...
not any or inherit..
I get my wp editors back but dont get any result from the query...
I'm a missing something here?
Found a fix! Changed WP_Query to query_posts() if someone else faces the same problem...
$args = array(
'post_type' => 'attachment',
'post_status' => 'inherit',
'posts_per_page' => -1,
'post_mime_type' =>'application/pdf, image/jpeg, image/gif, image/jpg, image/png'
);
$tmp = $post;
// The Query
$the_query2 = query_posts( $args );
if ( $the_query2 ) {
echo '<select class="bw_selector" name="bw_vendor_material">';
echo '<option value="">Choose attachment:</option>';
// The Loop
foreach ($the_query2 as $key => $value) {
//print_r($value->ID);
echo '<option value="'.$value->ID.'">' . $value->post_mime_type .': '. get_the_title($value->ID) . '</option>';
}
echo '</select>';
echo '<br/>';
}
else {
echo "No posts found";
}
// Restore original Post Data
wp_reset_query();
$post = $tmp;

What is the function got get all the media files wordpress?

Can anyone suggest me what is the function to get all the images stored for wordpress? I just need to list all the images seen under menu Media of the wordpress admin.
Thanks in advance
Uploaded images are stored as posts with the type "attachment"; use get_posts() with the right parameters. In the Codex entry for get_posts(), this example:
<?php
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => null, // any parent
);
$attachments = get_posts($args);
if ($attachments) {
foreach ($attachments as $post) {
setup_postdata($post);
the_title();
the_attachment_link($post->ID, false);
the_excerpt();
}
}
?>
...loops through all the attachments and displays them.
If you just want to get images, as TheDeadMedic commented, you can filter with 'post_mime_type' => 'image' in the arguments.
<ul>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => $post->ID
);
$attachments = get_posts( $args );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
echo '<li>';
echo wp_get_attachment_image( $attachment->ID, 'full' );
echo '<p>';
echo apply_filters( 'the_title', $attachment->post_title );
echo '</p></li>';
}
}
endwhile; endif; ?>
</ul>

Resources