$wpdb->prefix

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

$wpdb->prefix

Haluk Karamete
I installed 2 WordPress sites but pointed the 2nd WordPress install to the
1st one's database. Since I used a different table prefix for the 2nd one,
everything works.

This results that  I got 2 set of WordPress tables in the same database
while the 2nd one uses just a different wpdb prefix.

The question is...

How can I use the get_posts() ( or WP_Query for that matter) to query to
1st WordPress's posts from the 2nd one?

Is there an argument that I can pass to the get_posts or to WP_query to
tell the WP to use a different prefix than the current installs
wpdb->prefix default?


Ideally, this ios what I'm looking for;

$args = array(
'db_prefix' => 'xyz',
'posts_per_page' => -1,
);

Is there something like the 'db_prefix' above ?
_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers
Reply | Threaded
Open this post in threaded view
|

Re: $wpdb->prefix

Christian Wach
Sounds to me like you should host both sites in a multisite instance.

Cheers,

Christian


On 13 Jun 2015, at 03:06, Haluk Karamete <[hidden email]> wrote:

> I installed 2 WordPress sites but pointed the 2nd WordPress install to the
> 1st one's database. Since I used a different table prefix for the 2nd one,
> everything works.
>
> This results that  I got 2 set of WordPress tables in the same database
> while the 2nd one uses just a different wpdb prefix.
>
> The question is...
>
> How can I use the get_posts() ( or WP_Query for that matter) to query to
> 1st WordPress's posts from the 2nd one?
>
> Is there an argument that I can pass to the get_posts or to WP_query to
> tell the WP to use a different prefix than the current installs
> wpdb->prefix default?
>
>
> Ideally, this ios what I'm looking for;
>
> $args = array(
> 'db_prefix' => 'xyz',
> 'posts_per_page' => -1,
> );
>
> Is there something like the 'db_prefix' above ?
> _______________________________________________
> wp-hackers mailing list
> [hidden email]
> http://lists.automattic.com/mailman/listinfo/wp-hackers

_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers
Reply | Threaded
Open this post in threaded view
|

Re: $wpdb->prefix

J.D. Grimes
Depending on what you are doing, you might not want or need multisite. In that case, this should work:



$old_prefix = $wpdb->set_prefix( $site_1_prefix );

// get posts, etc., here.

$wpdb->set_prefix( $old_prefix );



See wpdb::set_prefix(): https://developer.wordpress.org/reference/classes/wpdb/set_prefix/

-J.D.

> On Jun 13, 2015, at 6:10 AM, Christian Wach <[hidden email]> wrote:
>
> Sounds to me like you should host both sites in a multisite instance.
>
> Cheers,
>
> Christian
>
>
> On 13 Jun 2015, at 03:06, Haluk Karamete <[hidden email]> wrote:
>
>> I installed 2 WordPress sites but pointed the 2nd WordPress install to the
>> 1st one's database. Since I used a different table prefix for the 2nd one,
>> everything works.
>>
>> This results that  I got 2 set of WordPress tables in the same database
>> while the 2nd one uses just a different wpdb prefix.
>>
>> The question is...
>>
>> How can I use the get_posts() ( or WP_Query for that matter) to query to
>> 1st WordPress's posts from the 2nd one?
>>
>> Is there an argument that I can pass to the get_posts or to WP_query to
>> tell the WP to use a different prefix than the current installs
>> wpdb->prefix default?
>>
>>
>> Ideally, this ios what I'm looking for;
>>
>> $args = array(
>> 'db_prefix' => 'xyz',
>> 'posts_per_page' => -1,
>> );
>>
>> Is there something like the 'db_prefix' above ?
>> _______________________________________________
>> wp-hackers mailing list
>> [hidden email]
>> http://lists.automattic.com/mailman/listinfo/wp-hackers
>
> _______________________________________________
> wp-hackers mailing list
> [hidden email]
> http://lists.automattic.com/mailman/listinfo/wp-hackers

_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers
Reply | Threaded
Open this post in threaded view
|

Re: $wpdb->prefix

Stephen Harris
This method isn't without it's difficulties, because of caching and
globals and the like.

You could attempt to mimic WordPress' switch_to_bog() adapted for a
non-multisite instance. E.g. flush cache, reinitialise use roles etc.
This would involve flushing the cache completely
($wp_object_cache->switch_to_blog()) won't work here because you're not
in a multisite environment).

But I'm not convinced it would be plain sailing from there either :).

Depending on what is required, simply reading the first site's RSS feed
and rendering that might the most straightforward solution.

Stephen


On 13/06/15 14:04, J.D. Grimes wrote:

> Depending on what you are doing, you might not want or need multisite. In that case, this should work:
>
>
>
> $old_prefix = $wpdb->set_prefix( $site_1_prefix );
>
> // get posts, etc., here.
>
> $wpdb->set_prefix( $old_prefix );
>
>
>
> See wpdb::set_prefix(): https://developer.wordpress.org/reference/classes/wpdb/set_prefix/
>
> -J.D.

_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers
Reply | Threaded
Open this post in threaded view
|

Re: $wpdb->prefix

Haluk Karamete
Actually using wpdb->get_results could have just done the job for me ( but
I need a more complex SQL that a simple one as follows )

 $SQL = "
    SELECT other_prefix_posts.*
    FROM other_prefix_posts
    WHERE ( other_prefix_posts.ID < 10 )
    AND other_prefix_posts.post_status = 'publish'
    AND other_prefix_posts.post_type = 'post'
    ORDER BY other_prefix_posts.post_date DESC
 ";

 $posts = $wpdb->get_results($querystr, OBJECT);

This won't cut my need cause I need post_tags and category terms sprinkled
over this SQL & that's where the buck stops.







On Sat, Jun 13, 2015 at 9:02 AM, Stephen Harris <[hidden email]>
wrote:

> This method isn't without it's difficulties, because of caching and
> globals and the like.
>
> You could attempt to mimic WordPress' switch_to_bog() adapted for a
> non-multisite instance. E.g. flush cache, reinitialise use roles etc. This
> would involve flushing the cache completely
> ($wp_object_cache->switch_to_blog()) won't work here because you're not in
> a multisite environment).
>
> But I'm not convinced it would be plain sailing from there either :).
>
> Depending on what is required, simply reading the first site's RSS feed
> and rendering that might the most straightforward solution.
>
> Stephen
>
>
> On 13/06/15 14:04, J.D. Grimes wrote:
>
>> Depending on what you are doing, you might not want or need multisite. In
>> that case, this should work:
>>
>>
>>
>> $old_prefix = $wpdb->set_prefix( $site_1_prefix );
>>
>> // get posts, etc., here.
>>
>> $wpdb->set_prefix( $old_prefix );
>>
>>
>>
>> See wpdb::set_prefix():
>> https://developer.wordpress.org/reference/classes/wpdb/set_prefix/
>>
>> -J.D.
>>
>
> _______________________________________________
> wp-hackers mailing list
> [hidden email]
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>
_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers
Reply | Threaded
Open this post in threaded view
|

Re: $wpdb->prefix

Chris McCoy-2
you would apply a join

http://wordpress.stackexchange.com/questions/8503/optimize-multiple-taxonomy-term-mysql-query 


here are a few examples in here.






On 2015-06-13, 4:27 PM, "wp-hackers on behalf of Haluk Karamete" <[hidden email] on behalf of [hidden email]> wrote:

>Actually using wpdb->get_results could have just done the job for me ( but
>I need a more complex SQL that a simple one as follows )
>
> $SQL = "
>    SELECT other_prefix_posts.*
>    FROM other_prefix_posts
>    WHERE ( other_prefix_posts.ID < 10 )
>    AND other_prefix_posts.post_status = 'publish'
>    AND other_prefix_posts.post_type = 'post'
>    ORDER BY other_prefix_posts.post_date DESC
> ";
>
> $posts = $wpdb->get_results($querystr, OBJECT);
>
>This won't cut my need cause I need post_tags and category terms sprinkled
>over this SQL & that's where the buck stops.
>
>
>
>
>
>
>
>On Sat, Jun 13, 2015 at 9:02 AM, Stephen Harris <[hidden email]>
>wrote:
>
>> This method isn't without it's difficulties, because of caching and
>> globals and the like.
>>
>> You could attempt to mimic WordPress' switch_to_bog() adapted for a
>> non-multisite instance. E.g. flush cache, reinitialise use roles etc. This
>> would involve flushing the cache completely
>> ($wp_object_cache->switch_to_blog()) won't work here because you're not in
>> a multisite environment).
>>
>> But I'm not convinced it would be plain sailing from there either :).
>>
>> Depending on what is required, simply reading the first site's RSS feed
>> and rendering that might the most straightforward solution.
>>
>> Stephen
>>
>>
>> On 13/06/15 14:04, J.D. Grimes wrote:
>>
>>> Depending on what you are doing, you might not want or need multisite. In
>>> that case, this should work:
>>>
>>>
>>>
>>> $old_prefix = $wpdb->set_prefix( $site_1_prefix );
>>>
>>> // get posts, etc., here.
>>>
>>> $wpdb->set_prefix( $old_prefix );
>>>
>>>
>>>
>>> See wpdb::set_prefix():
>>> https://developer.wordpress.org/reference/classes/wpdb/set_prefix/
>>>
>>> -J.D.
>>>
>>
>> _______________________________________________
>> wp-hackers mailing list
>> [hidden email]
>> http://lists.automattic.com/mailman/listinfo/wp-hackers
>>
>_______________________________________________
>wp-hackers mailing list
>[hidden email]
>http://lists.automattic.com/mailman/listinfo/wp-hackers

_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers
Xen
Reply | Threaded
Open this post in threaded view
|

Re: $wpdb->prefix

Xen
In reply to this post by Haluk Karamete
Yes you can apply a few joins.

The database structure is fairly easily set up. I believe you need to do
two joins to marry the terms with the term_taxonomy (terms contains the
name and the slug, term_taxonomy the description and the
term_taxonomy_id (which is usually the same) which is then used in the
...) and the term_relationships.

In term_relationships you find the taxonomy_id coupled to post_ids
(object_id).

So your JOIN will be something of the kind that:

SELECT ... FROM ... AS p JOIN prefix_term_relationships AS r ON p.ID =
r.object_id JOIN prefix_term_taxonomy AS tax ON r.term_taxonomy_id =
tax.term_taxonomy_id JOIN prefix_terms AS term ON tax.term_id = term.term_ID

That's three.

In your statement you then have access to term.slug (for example) or
tax.taxonomy (to differentiate tags and categories).

You can probably write this nicer with

SELECT ... FROM ... AS p JOIN (prefix_term_relationships AS,
prefix_term_taxonomy AS tax, prefix_terms AS term) ON (r ON p.ID =
r.object_id AND r.term_taxonomy_id = tax.term_taxonomy_id AND
tax.term_id = term.term_ID)

If you need extra material from the other tables, apart from just a
WHERE check, you may need to make an inner statement:

SELECT .., ..., .. FROM ( ... )

but I'm not sure about that, I'm sure you can just supply:

SELECT p.ID, term.slug FROM ..... JOIN ( .... ) ON ( ... )

Good luck.

Bart.


On 13-06-15 22:27, Haluk Karamete wrote:

> Actually using wpdb->get_results could have just done the job for me ( but
> I need a more complex SQL that a simple one as follows )
>
>   $SQL = "
>      SELECT other_prefix_posts.*
>      FROM other_prefix_posts
>      WHERE ( other_prefix_posts.ID < 10 )
>      AND other_prefix_posts.post_status = 'publish'
>      AND other_prefix_posts.post_type = 'post'
>      ORDER BY other_prefix_posts.post_date DESC
>   ";
>
>   $posts = $wpdb->get_results($querystr, OBJECT);
>
> This won't cut my need cause I need post_tags and category terms sprinkled
> over this SQL & that's where the buck stops.
>
>
>
>
>
>
>
> On Sat, Jun 13, 2015 at 9:02 AM, Stephen Harris <[hidden email]>
> wrote:
>
>> This method isn't without it's difficulties, because of caching and
>> globals and the like.
>>
>> You could attempt to mimic WordPress' switch_to_bog() adapted for a
>> non-multisite instance. E.g. flush cache, reinitialise use roles etc. This
>> would involve flushing the cache completely
>> ($wp_object_cache->switch_to_blog()) won't work here because you're not in
>> a multisite environment).
>>
>> But I'm not convinced it would be plain sailing from there either :).
>>
>> Depending on what is required, simply reading the first site's RSS feed
>> and rendering that might the most straightforward solution.
>>
>> Stephen
>>
>>
>> On 13/06/15 14:04, J.D. Grimes wrote:
>>
>>> Depending on what you are doing, you might not want or need multisite. In
>>> that case, this should work:
>>>
>>>
>>>
>>> $old_prefix = $wpdb->set_prefix( $site_1_prefix );
>>>
>>> // get posts, etc., here.
>>>
>>> $wpdb->set_prefix( $old_prefix );
>>>
>>>
>>>
>>> See wpdb::set_prefix():
>>> https://developer.wordpress.org/reference/classes/wpdb/set_prefix/
>>>
>>> -J.D.
>>>
>> _______________________________________________
>> wp-hackers mailing list
>> [hidden email]
>> http://lists.automattic.com/mailman/listinfo/wp-hackers
>>
> _______________________________________________
> wp-hackers mailing list
> [hidden email]
> http://lists.automattic.com/mailman/listinfo/wp-hackers

_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers
Xen
Reply | Threaded
Open this post in threaded view
|

Re: $wpdb->prefix

Xen
Slight error in this SQL:


SELECT ... FROM ... AS p JOIN (prefix_term_relationships AS,
prefix_term_taxonomy AS tax, prefix_terms AS term) ON (p.ID =
r.object_id AND r.term_taxonomy_id = tax.term_taxonomy_id AND
tax.term_id = term.term_ID)


Fixed.


On 14-06-15 09:29, Xen wrote:

> Yes you can apply a few joins.
>
> The database structure is fairly easily set up. I believe you need to
> do two joins to marry the terms with the term_taxonomy (terms contains
> the name and the slug, term_taxonomy the description and the
> term_taxonomy_id (which is usually the same) which is then used in the
> ...) and the term_relationships.
>
> In term_relationships you find the taxonomy_id coupled to post_ids
> (object_id).
>
> So your JOIN will be something of the kind that:
>
> SELECT ... FROM ... AS p JOIN prefix_term_relationships AS r ON p.ID =
> r.object_id JOIN prefix_term_taxonomy AS tax ON r.term_taxonomy_id =
> tax.term_taxonomy_id JOIN prefix_terms AS term ON tax.term_id =
> term.term_ID
>
> That's three.
>
> In your statement you then have access to term.slug (for example) or
> tax.taxonomy (to differentiate tags and categories).
>
> You can probably write this nicer with
>
> SELECT ... FROM ... AS p JOIN (prefix_term_relationships AS,
> prefix_term_taxonomy AS tax, prefix_terms AS term) ON (r ON p.ID =
> r.object_id AND r.term_taxonomy_id = tax.term_taxonomy_id AND
> tax.term_id = term.term_ID)
>
> If you need extra material from the other tables, apart from just a
> WHERE check, you may need to make an inner statement:
>
> SELECT .., ..., .. FROM ( ... )
>
> but I'm not sure about that, I'm sure you can just supply:
>
> SELECT p.ID, term.slug FROM ..... JOIN ( .... ) ON ( ... )
>
> Good luck.
>
> Bart.
>
>
> On 13-06-15 22:27, Haluk Karamete wrote:
>> Actually using wpdb->get_results could have just done the job for me
>> ( but
>> I need a more complex SQL that a simple one as follows )
>>
>>   $SQL = "
>>      SELECT other_prefix_posts.*
>>      FROM other_prefix_posts
>>      WHERE ( other_prefix_posts.ID < 10 )
>>      AND other_prefix_posts.post_status = 'publish'
>>      AND other_prefix_posts.post_type = 'post'
>>      ORDER BY other_prefix_posts.post_date DESC
>>   ";
>>
>>   $posts = $wpdb->get_results($querystr, OBJECT);
>>
>> This won't cut my need cause I need post_tags and category terms
>> sprinkled
>> over this SQL & that's where the buck stops.
>>
>>
>>
>>
>>
>>
>>
>> On Sat, Jun 13, 2015 at 9:02 AM, Stephen Harris
>> <[hidden email]>
>> wrote:
>>
>>> This method isn't without it's difficulties, because of caching and
>>> globals and the like.
>>>
>>> You could attempt to mimic WordPress' switch_to_bog() adapted for a
>>> non-multisite instance. E.g. flush cache, reinitialise use roles
>>> etc. This
>>> would involve flushing the cache completely
>>> ($wp_object_cache->switch_to_blog()) won't work here because you're
>>> not in
>>> a multisite environment).
>>>
>>> But I'm not convinced it would be plain sailing from there either :).
>>>
>>> Depending on what is required, simply reading the first site's RSS feed
>>> and rendering that might the most straightforward solution.
>>>
>>> Stephen
>>>
>>>
>>> On 13/06/15 14:04, J.D. Grimes wrote:
>>>
>>>> Depending on what you are doing, you might not want or need
>>>> multisite. In
>>>> that case, this should work:
>>>>
>>>>
>>>>
>>>> $old_prefix = $wpdb->set_prefix( $site_1_prefix );
>>>>
>>>> // get posts, etc., here.
>>>>
>>>> $wpdb->set_prefix( $old_prefix );
>>>>
>>>>
>>>>
>>>> See wpdb::set_prefix():
>>>> https://developer.wordpress.org/reference/classes/wpdb/set_prefix/
>>>>
>>>> -J.D.
>>>>
>>> _______________________________________________
>>> wp-hackers mailing list
>>> [hidden email]
>>> http://lists.automattic.com/mailman/listinfo/wp-hackers
>>>
>> _______________________________________________
>> wp-hackers mailing list
>> [hidden email]
>> http://lists.automattic.com/mailman/listinfo/wp-hackers
>
> _______________________________________________
> wp-hackers mailing list
> [hidden email]
> http://lists.automattic.com/mailman/listinfo/wp-hackers

_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers